CS 111A Lecture Notes - Random Number Generation - Section 4.9
- To generate a random number, use the Math.random()
function. It returns a number between 0
and 1 (greater than or equal to 0, but less than 1). You can
multiply that result times any number to get
a random number between 0 and your multiplier. For example, the
following line will make id store a random
integer between 0 and max-1 (inclusive):
int id = (int)(max * Math.random());
- In-class exercise: Write a program to generate a random integer
between 5 and 10 (inclusive) and show it on the screen.
- RandomSimpleExercise.java:
solution to above in-class exercise.
Return to CS 111A page