FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Bugs
   Website, Reference, Example Bugs
(Moderator: REAS)
   random() definition
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: random() definition  (Read 525 times)
Quasimondo

WWW Email
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
TomC

WWW
Re: random() definition
« Reply #1 on: Mar 29th, 2004, 1:41pm »

I believe fry has changed this to work as you expect in release 69.
 
Some discussion here...
 
http://processing.org/discourse/yabb/board_Syntax_action_displa_y_num_1079935258.html
 
fry

WWW
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)
 
Pages: 1 

« Previous topic | Next topic »