280 likes | 296 Views
Learn how to simulate random events like rolling dice, playing cards, and tossing coins using Java for loops and pseudo-random number generators. Understand the concept of fairness in simulations.
E N D
Repetition The for loop
Problem: simulating some event • Rolling dice • Playing cards • FPS game (first person shooter) • Nuclear reactions • PET (positron emission tomography)
(pseudo) random number generator • Similar to reading from input, we have a random class from which we can read random numbers. • To use the random number generator, we must first: import java.util.Random; … Random r = new Random();
Random number generator Random r = new Random(); //toss a coin - true = heads, false = tails boolean heads = r.nextBoolean(); //toss if (heads) { … } … heads = r.nextBoolean(); //toss again
Random number generator Random r = new Random(); //roll dice (actually one die) How? What are the range of values? What is the type of a value?
Random number generator Random r = new Random(); //roll dice (actually one die) int roll = r.nextInt(); If we try this a few times and print the result, we see that we get a possibly large int that can be negative or positive (or zero). How can we make that 1..6?
Random number generator Random r = new Random(); //roll dice (actually one die) int roll = r.nextInt(); if (roll<1) roll = 1; if (roll>6) roll = 6; Does this simulate a die well? First, let’s deal with the possibility of a negative number.
Random number generator Random r = new Random(); //roll dice (actually one die) int roll = r.nextInt(); if (roll < 0) roll = -roll; //we could also use ?:
Random number generator Random r = new Random(); //roll dice (actually one die) int roll = r.nextInt(); roll = (roll>=0) ? roll : -roll; //make non neg Now our roll is in [0..+N] where N is a big int. How can we make it 1..6 (or 0..5)?
Random number generator Random r = new Random(); //roll dice (actually one die) int roll = r.nextInt(); roll = (roll>=0) ? roll : -roll; //make non neg roll = roll % 6; //constrain to [0..5] roll++; //now in [1..6]
Random number generator Random r = new Random(); //roll dice (actually one die) int roll = r.nextInt(); roll = (roll>=0) ? roll : -roll; //make non neg roll = roll % 6; //constrain to [0..5] roll++; //now in [1..6] How can we use this to simulate the tossing of a coin (instead of using nextBoolean)?
Random number generator Random r = new Random(); //toss a coin int toss = r.nextInt(); roll = (roll>=0) ? roll : -roll; //make non neg roll = roll % 2; //constrain to [0..1] if (roll==0) { //heads } else { //tails } But nextBoolean() is easier!
Random number generator //simulate the tossing of a coin Random toss = new Random(); //toss a coin 5 times boolean t1, t2, t3, t4, t5; t1 = toss.nextBoolean(); t2 = toss.nextBoolean(); t3 = toss.nextBoolean(); t4 = toss.nextBoolean(); t5 = toss.nextBoolean();
But is the coin “fair?” • If the coin is “fair” then half of the time it will come up heads and half of the time it will come up tails. • So how do we know if a coin is fair? • Toss it a bazillion times and if it comes up heads a half a bazillion times and tails a half a bazillion times, then it is fair. • How do we know if our computer-simulated-coin is fair?
Determine if a simulated coin is fair. Random toss = new Random(); int headCount = 0; //counts heads int tailCount = 0; //counts tails boolean isHeads; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1;
Determine if a simulated coin is fair. isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1;
isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; But I need to do this a bazillion times!
Introducing the for loop for (int i=0; i<100000; i++) { //this will be repeated many times //the value of i will change each time. System.out.println( "hello " + i ); }
for loop in general for (initialization; boolean_expression; update) { } So how can we use this to simulate a coin and determine if our random number generator is fair?
Remember how we tossed a coin once. Random toss = new Random(); int headCount = 0; //counts heads int tailCount = 0; //counts tails boolean isHeads; isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1;
Is our simulated coin fair? for (int i=0; i<1000000; i++) { isHeads = toss.nextBoolean(); if (isHeads) headCount = headCount + 1; else tailCount = tailCount + 1; } System.out.println( "heads came up " + headCount + " times." ); System.out.println( "tails came up " + tailCount + " times." );
Old problem of the week: • I roll my dice. • My friend rolls her dice. • What is the chance (probability) that the sum of my dice is equal to the sum of my friends dice? • Ex. • I roll 6 and 2. My friend rolls 4 and 4. That’s a match because 6+2 = 4+4 = 8. What’s the chance of this happening?
Old problem of the week. • I can do the algebra, but can I write a program to simulate this and use the result to check my answer?
import java.util.Random; class Simulate { public static void main ( String[] p ) { //final int count = 20000000; //define bazillion final int count = 100; //define bazillion int my1, my2; //declare my dice int her1, her2; //declare her dice //declare our random number generator which // simulates the roll of dice. Random roll = new Random();
. . . //roll the dice many, many times System.out.println( "let's roll!" ); int same = 0; for (int i=1; i<=count; i++) { my1 = roll.nextInt(); my1 = (my1>=0) ? (my1%6+1) : (-my1%6+1); my2 = roll.nextInt(); my2 = (my2>=0) ? (my2%6+1) : (-my2%6+1); her1 = roll.nextInt(); her1 = (her1>=0) ? (her1%6+1) : (-her1%6+1); her2 = roll.nextInt(); her2 = (her2>=0) ? (her2%6+1) : (-her2%6+1); int mySum = my1 + my2; int herSum = her1 + her2; if (mySum==herSum) same = same + 1; System.out.println( "i=" + i + ": (" + my1 + "," + my2 + ") (" + her1 + "," + her2 + ")" ); } . . .
. . . //report results double dCount = (double)same / count; System.out.println( "probability = " + dCount ); //finish up System.out.println( "aloha." ); } }
import java.util.Random; class Simulate { public static void main ( String[] p ) { //final int count = 20000000; //define bazillion final int count = 100; //define bazillion int my1, my2; //declare my dice int her1, her2; //declare her dice //declare our random number generator which simulates the roll of dice. Random roll = new Random(); //roll the dice many, many times System.out.println( "let's roll!" ); int same = 0; for (int i=1; i<=count; i++) { my1 = roll.nextInt(); my1 = (my1>=0) ? (my1%6+1) : (-my1%6+1); my2 = roll.nextInt(); my2 = (my2>=0) ? (my2%6+1) : (-my2%6+1); her1 = roll.nextInt(); her1 = (her1>=0) ? (her1%6+1) : (-her1%6+1); her2 = roll.nextInt(); her2 = (her2>=0) ? (her2%6+1) : (-her2%6+1); int mySum = my1 + my2; int herSum = her1 + her2; if (mySum==herSum) same = same + 1; System.out.println( "i=" + i + ": (" + my1 + "," + my2 + ") (" + her1 + "," + her2 + ")" ); } //report results double dCount = (double)same / count; System.out.println( "probability = " + dCount ); //finish up System.out.println( "aloha." ); } } Complete program that one may run.