# Random() limit : high values required

**URL:** <https://discourse.processing.org/t/random-limit-high-values-required/27927>\
**Category:** Beginners\
**Created:** [February 19, 2021, 7:33am UTC](https://discourse.processing.org/t/random-limit-high-values-required/27927 "2021-02-19T07:33:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![IRIGIMA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/irigima/32/8951_2.png) [@IRIGIMA](https://discourse.processing.org/u/IRIGIMA)\
**Post date:** [February 19, 2021, 7:33am UTC](https://discourse.processing.org/t/random-limit-high-values-required/27927/1 "2021-02-19T07:33:03Z")

</div>

Does anyone know the upper limit for the random() function ??

I’m needing 400,000,000 +

Solution would be:  
A=random(20000)  
B=random(20000)  
If (A & B)=X

(Or any upper limit required)  
But this is messy.

Any thoughts ??

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [February 19, 2021, 8:19am UTC](https://discourse.processing.org/t/random-limit-high-values-required/27927/2 "2021-02-19T08:19:45Z")

</div>

You can do it by stacking random values.

```auto
void setup() {println((getRandom(10)));} void draw() {}
String getRandom(int len) {
  String output = "";
  for(int i = 0; i < len; i++) output += floor(random(9.999));
  return output;
}

```

I will add an integer version soon.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [February 19, 2021, 8:22am UTC](https://discourse.processing.org/t/random-limit-high-values-required/27927/3 "2021-02-19T08:22:56Z")

</div>

here is the `<long>` version of it (long is an integer with larger range)

```auto
void setup() {println((getRandom(10)));} void draw() {}
long getRandom(int len) {
  int output = 0;
  for(int i = 0; i < len; i++) {output = output*10 + floor(random(10)); println(output);}
  return output;
}

```

edit:  
the len value is 10^len. (if you want random int between 1 and 1000, use getRandom(3));  
essentially it is 1e[len] ( 1 \* 10^len)

---

<div class="post-metadata">

**Author:** ![IRIGIMA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/irigima/32/8951_2.png) [@IRIGIMA](https://discourse.processing.org/u/IRIGIMA)\
**Post date:** [February 19, 2021, 1:44pm UTC](https://discourse.processing.org/t/random-limit-high-values-required/27927/4 "2021-02-19T13:44:02Z")

</div>

As I expected. No function to use. Think I’ll just go as per initial example.  
Needs to be as fast as possible.

Thanks for alternative solution.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [February 19, 2021, 3:59pm UTC](https://discourse.processing.org/t/random-limit-high-values-required/27927/5 "2021-02-19T15:59:47Z")

</div>

Hi,

Internally this is what the `random()` function looks like :

```auto
public final float random(float high) {
    // avoid an infinite loop when 0 or NaN are passed in
    if (high == 0 || high != high) {
      return 0;
    }

    if (internalRandom == null) {
      internalRandom = new Random();
    }

    // for some reason (rounding error?) Math.random() * 3
    // can sometimes return '3' (once in ~30 million tries)
    // so a check was added to avoid the inclusion of 'howbig'
    float value = 0;
    do {
      value = internalRandom.nextFloat() * high;
    } while (value == high);
    return value;
  }

```

This is creating an instance of the [`java.util.Random`](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html) class which then calls the `nextFloat()` function :

> `nextFloat()`
> 
> Returns the next pseudorandom, uniformly distributed `float` value between `0.0` and `1.0` from this random number generator’s sequence.

So multiplying two floats together gives another float and their range is :

> float: 4 bytes, IEEE 754. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative).

(from [types - What is the inclusive range of float and double in Java? - Stack Overflow](https://stackoverflow.com/questions/1650505/what-is-the-inclusive-range-of-float-and-double-in-java))

So using Processing random function is perfectly fine for this case :

```auto
import java.util.Random;

Random rng = new Random();

final float MAX_RANDOM = 4e8;

println(random(MAX_RANDOM)); // -> 2.10096768E8
println(rng.nextFloat() * MAX_RANDOM); // -> 3.96103776E8 (equivalent)

```

As stated in the code, this can lead to rounding error but they are handled and are very rare. 😉

---

<div class="post-metadata">

**Author:** ![IRIGIMA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/irigima/32/8951_2.png) [@IRIGIMA](https://discourse.processing.org/u/IRIGIMA)\
**Post date:** [February 19, 2021, 5:16pm UTC](https://discourse.processing.org/t/random-limit-high-values-required/27927/6 "2021-02-19T17:16:54Z")

</div>

Thank you all so much for your help and example code.

I’m basically running an iteration (~ 10 million per update) : currently ~ 1 second within code subs.  
~ 10 to 1,000 updates, I require one go sub .

If (random(100,000,000)=1) for instance for 10 updates as per example (10 X 10 million) will go sub ~ 1/100 million

All the requirement. But random(X) has a limit X \< 10 million.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [February 19, 2021, 5:34pm UTC](https://discourse.processing.org/t/random-limit-high-values-required/27927/7 "2021-02-19T17:34:20Z")

</div>

> [@josephh](#):
>
> ```auto
> do {
> value = internalRandom.nextFloat() * high;
> } while (value == high);
> 
> ```

what is the difference between `while` and `do { } while();`

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [February 19, 2021, 5:41pm UTC](https://discourse.processing.org/t/random-limit-high-values-required/27927/8 "2021-02-19T17:41:14Z")

</div>

The `do {} while {};` statement is a regular `while {}` but the body is executed once at the beginning.

In this case it is useful because we want to get a first random value and if it’s wrong we do it again otherwise we return it.

With a regular while, we would need to do the following :

```auto
float value = internalRandom.nextFloat() * high;

while (value == high) {
  value = internalRandom.nextFloat() * high;
} 

```

Which is duplicating the same line twice 😉

From the [Oracle doc](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html) :

> The difference between `do-while` and `while` is that `do-while` evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the `do` block are always executed at least once

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [February 19, 2021, 5:45pm UTC](https://discourse.processing.org/t/random-limit-high-values-required/27927/9 "2021-02-19T17:45:47Z")

</div>

interesting!

Thank you for showing it to me!

---

<div class="post-metadata">

**Author:** ![IRIGIMA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/irigima/32/8951_2.png) [@IRIGIMA](https://discourse.processing.org/u/IRIGIMA)\
**Post date:** [February 24, 2021, 12:10am UTC](https://discourse.processing.org/t/random-limit-high-values-required/27927/10 "2021-02-24T00:10:24Z")

</div>

A quick thanks towards helping.  
Here’s a quick test sample rendered today using “Processing”

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a210052a500e74e824499503f171c1e940035ea1.jpeg)
