1 / 28

Repetition

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.

mcain
Download Presentation

Repetition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Repetition The for loop

  2. Problem: simulating some event • Rolling dice • Playing cards • FPS game (first person shooter) • Nuclear reactions • PET (positron emission tomography)

  3. (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();

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

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

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

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

  8. 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 ?:

  9. 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)?

  10. 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]

  11. 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)?

  12. 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!

  13. 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();

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

  15. 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;

  16. 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;

  17. 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!

  18. 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 ); }

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

  20. 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;

  21. 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." );

  22. An old problem of the week

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

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

  25. 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();

  26. . . . //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 + ")" ); } . . .

  27. . . . //report results double dCount = (double)same / count; System.out.println( "probability = " + dCount ); //finish up System.out.println( "aloha." ); } }

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

More Related