# Random function in Painless?

**URL:** <https://discuss.elastic.co/t/random-function-in-painless/70280>\
**Category:** Elasticsearch\
**Created:** [December 30, 2016, 2:50pm UTC](https://discuss.elastic.co/t/random-function-in-painless/70280 "2016-12-30T14:50:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dom-nik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dom-nik/32/13243_2.png) [@Dom-nik](https://discuss.elastic.co/u/Dom-nik)\
**Post date:** [December 30, 2016, 2:50pm UTC](https://discuss.elastic.co/t/random-function-in-painless/70280/1 "2016-12-30T14:50:35Z")

</div>

Hello,  
What is the way to genereate a random value in Painless?

Being able to draw one value from a set would be particularly great - I want to use it to add a new field to my mock data and I need a possibility to add one of the statuses ('Working', 'Archived', 'InReview') to my documents at random.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [December 30, 2016, 3:09pm UTC](https://discuss.elastic.co/t/random-function-in-painless/70280/2 "2016-12-30T15:09:02Z")

</div>

You have access to [Random](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html) in painless. Alternatively, if you have something like a timestamp in your mock data, you can calculate something like `timestamp % 3` and assign `'Working'`, `'Archived'`, or `'InReview'` based on the value.

---

<div class="post-metadata">

**Author:** ![Dom-nik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dom-nik/32/13243_2.png) [@Dom-nik](https://discuss.elastic.co/u/Dom-nik)\
**Post date:** [December 30, 2016, 3:41pm UTC](https://discuss.elastic.co/t/random-function-in-painless/70280/3 "2016-12-30T15:41:04Z")

</div>

Thank you, that's what I was looking for!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 27, 2017, 3:41pm UTC](https://discuss.elastic.co/t/random-function-in-painless/70280/4 "2017-01-27T15:41:10Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![lwintergerst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lwintergerst/32/18164_2.png) [@lwintergerst](https://discuss.elastic.co/u/lwintergerst)\
**Post date:** [August 24, 2023, 6:25am UTC](https://discuss.elastic.co/t/random-function-in-painless/70280/5 "2023-08-24T06:25:09Z")

</div>

Just commenting here so we have a complete example to copy/paste for anyone else reading this thread:

```auto
# choose random element from array with 6 entries:
PUT _ingest/pipeline/random_element_pipeline
{
  "processors": [
    {
      "script": {
        "source": """ 
        Random r = new Random();
        ctx.region = ctx.region[r.nextInt(6) - 1];
        """
      }
    }
  ]
}

```

You have access to

```auto
class java.util.Random {
  () @nondeterministic
  (long)
  DoubleStream doubles(long) @nondeterministic
  DoubleStream doubles(long,double,double) @nondeterministic
  IntStream ints(long) @nondeterministic
  IntStream ints(long,int,int) @nondeterministic
  LongStream longs(long) @nondeterministic
  LongStream longs(long,long,long) @nondeterministic
  boolean nextBoolean() @nondeterministic
  void nextBytes(byte[]) @nondeterministic
  double nextDouble() @nondeterministic
  float nextFloat() @nondeterministic
  double nextGaussian() @nondeterministic
  int nextInt() @nondeterministic
  int nextInt(int) @nondeterministic
  long nextLong() @nondeterministic
  void setSeed(long)
}

```
