Random() limit : high values required

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 ??

You can do it by stacking random values.

``````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.

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

``````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)

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.

Hi,

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

``````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` 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).

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

``````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.

2 Likes

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.

1 Like

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

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 :

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

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

Which is duplicating the same line twice

From the Oracle doc :

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

2 Likes

interesting!

Thank you for showing it to me!

1 Like

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

2 Likes