CS 1100 Lecture 13: Septemer 14, 2016 Notes

77 views2 pages
14 Sep 2016
School
Course
Professor

Document Summary

A number generated or chosen in such a way that it cannot be guessed. Rolling a die, flipping a coin, generating math problems. Returns a double greater than or equal to zero, but strictly less than 1. Often times we want a different range of random numbers by default. We can multiply a number to math. random() to change the range. Math. random() gives a double, but often we want an int. To do that, we would do: (int)(math. random()) this is wrong. Note than this will always give us 0. Need to scale the range, then cast to an int (int)(math. random() * 10) this is right. This gives a random integer in the range 0-9. So far, all our ranges started at 0. To shift the range, we simply add a number to the number generated (int)(math. random() * 10) +1. This leads to a random number between 1 and 10. General range of integers (int)(math. random() * b) + a.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents