|
Author |
Topic: random() definition (Read 525 times) |
|
Quasimondo
|
random() definition
« on: Mar 29th, 2004, 1:12pm » |
|
The documentation says that random(x) returns values BETWEEN 0 and x. The usual definition of the random function as I know it is, that it returns values up to x, but not including x. Thought the current implementation also returns x sometimes as you can see if you run the follwing example for a while: void loop(){ for (int i=0;i<100000;i++){ if (int(random(1))==1){ println("error"); } } } Problem with this show up if you are dealing with arrays, as it will give you OutOfBounds errors, if you rely on the usual definition of random(): int[] test=new int[10]; for (int i=0;i<100000;i++){ int dummy=test[int(random(10))]; } } So for the moment it would perhaps be a good idea to mention this in the documentation.
|
Mario Klingemann | Quasimondo | Incubator | côdeazur
|
|
|
fry
|
Re: random() definition
« Reply #2 on: Mar 29th, 2004, 4:58pm » |
|
the documentation is wrong, random(3) should be from [0,3) inclusive of 0, exclusive of 3. apparently due to rounding error, prior to 69 once every ~30 million attempts this would return a 3, so i've added code to avoid the situation. also for the other random, whcih will be random(low, high) -> [low, high). (i'm moving this over to the docs so that they get updated)
|
|
|
|
|