1 / 4

Random Numbers

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…

Download Presentation

Random Numbers

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

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

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

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

More Related