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)+smallest)
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
A boolean variable is a variable that can only have 2 possible values: true or false – See demo: BooleanDemo A boolean expression means something very different – it is simply a segment of code that is either true or false, such as: (score > 90) (name.equals(“Peppers”)) (letter == ‘a’) So, essentially any expression you might see after if or while is a boolean expression
Assignments 1. 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 37s – The average – The highest #, the lowest # – How many prime #s (your choice, you can do this the easy way [a bunch of ifs] or the challenging way: use a loop and a Boolean variable) – ***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 – Add a betting feature, where the player begins with $500, and can bet money on each turn. They double their bet if 2 numbers match, they quintuple their bet if all 3 numbers match, they lose their bet if no numbers match.