Unformatted text preview: Random numbers
Random numbers The random() function has two versions: random(5) returns a decimal value between 0 and up to but not including 5.0 random(5, 10.2) returns a decimal value between 5.0 and up to but not including 10.2. To drop the decimal part of a random number, use the int() function. int randNum = int(random(6));
int
int( randNum now holds an integer from 0 up to but not including 6: {0,1,2,3,4,5} 1 dice example
dice example Let's say I wanted to make an applet that simulated rolling a single six sided die
What numbers would be possible? 2 dice example
dice example Let's say I wanted to make an applet that simulated rolling a single six sided die
What numbers would be possible? {1,2,3,4,5,6} 3 dice example
dice example Let's say I wanted to make an applet that simulated rolling a single six sided die
What numbers would be possible? {1,2,3,4,5,6}
int roll = ??
int
?? 4 dice example
dice example Let's say I wanted to make an applet that simulated rolling a single six sided die
What numbers would be possible? {1,2,3,4,5,6}
int roll = int(random(?,?)); 5 dice example
dice example Let's say I wanted to make an applet that simulated rolling a single six sided die
What numbers would be possible? {1,2,3,4,5,6}
int roll = int(random(?,?));
6 dice example
dice example Let's say I wanted to make an applet that simulated rolling a single six sided die
What numbers would be possible? {1,2,3,4,5,6}
int roll = int(random(1,7));
7 A bug with negative arguments
A bug with negative arguments What arguments would you use to get this range? {3,2,1,0,1,2,3}
int roll = int(random(?,?)); 8 A bug with negative arguments
A bug with negative arguments For some reason the low limit is one less. {3,2,1,0,1,2,3}
int roll =int(random(4,4)); 9 Random Walk
Random Walk The position or state of an object changes unpredictably (by a random amount) over time
Software uses random walks to model many "real life" things including: Weather Stock Prices Load on a web server Chemistry (gas particles)
10 More Random Walk examples
More Random Walk examples
http://www.mit.edu/~kardar/teaching/projects/ch
emotaxis(AndreaSchmidt)/random.htm 11 12 A Random Walk isn't a Random A Random Jump
I'd call this a random jump
x = int(random(0,400)); 13 A Random Walk isn't a Random A Random Jump
I'd call this a random jump
x = int(random(0,400)); I'd call this a random walk
x = x + int(random(2,2)); 14 A onedimensional random walk
A The one dimension is the x axis, y doesn't change int x = 150;
void setup()
{
size(300,100);
size(300,100);
}
void draw()
{
background(0);
background(0);
ellipse(x,50,40,40);
ellipse(x,50,40,40);
x = x + int(random(2,2));
int(random(2,2));
} 15 Practice Quiz Questions
Practice Quiz Questions Fill in the blanks to create the indicated random numbers
A random integer from 0 to 10: (including 10)
int(random(________))
A random integer from 1 to 100: (including 100)
int(random(________))
A random integer from 5 to 5: (including 5)
int(random(________))
16 ...
View
Full Document
 Spring '08
 Ma
 Web server, gas particles, R.O.O.T.S., Dice Example

Click to edit the document details