50 likes | 195 Views
Random Numbers. Random numbers are extremely useful: especially for games, and also for calculating experimental probabilities. Formula for generating random integers: ( int )( Math.random ()*(how many #s)+minimum). examples. Creates a random # between…
E N D
Random Numbers • Random numbers are extremely useful: especially for games, and also for calculating experimental probabilities. • Formula for generating random integers: (int)(Math.random()*(how many #s)+minimum)
examples • Creates a random # between… x = (int)(Math.random()*6+1) // 1 to 6 y = (int)(Math.random()*7+50) // 50 to 56 z = (int)(Math.random()*21+100) // 100 to 120 Demo: RandomNumberDemo
Assignments1. Fifty • Use a for loop to create and display 12 random #s between 1 and 50. Then display the following: • How many odd #s, how many evens • How many #s in the 30s • How many 17s • The average • The highest #, the lowest # • How many prime #s (props if you do this the hard way) • ***Make sure your results are correct.*** see next slide…
2) p. 185 # 15 • Think about it – how can you use random numbers to decide the computer’s “choice”? • Each round, make sure to display what the computer chose, as well as who won or lost (or if they tied). • The user should be able to play as many rounds as they like. 3) p. 185 # 16