go to previous page   go to home page   go to next page hear noise highlighting

Answer:

Yes. For example, a solitaire player who has just lost a game might want to play the game again with the same shuffle of the deck to see if a better strategy leads to a win.


nextInt()

int nextInt() — Returns the next pseudorandom, uniformly distributed int value.
All possible int values, positive, zero, and negative, are in the range of values returned.
int nextInt( int num ) — Returns a pseudorandom, uniformly distributed int
value between 0 (inclusive) and num (exclusive).

Above are two methods (out of several) that Random objects provide. The second method is the one most often used.

num must be a positive integer. The random integers are selected from the range 0 up to and including num-1 but not including num.

For example,

int val = rand.nextInt(6);

might return 0, 1, 2, 3, 4, or 5.


QUESTION 5:

Say that you want pseudorandom numbers selected from the range 1 through 6.

How can nextInt( int num ) be used for that?


go to previous page   go to home page   go to next page