nextInt()

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

Yes. For example, a solitare player who has just lost a game might want to play the game again with the same shuffle of the deck.

nextInt()

Here are two methods (out of several) that Random objects provide:

int nextInt() — Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. All possible int values, both positive 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) from this random number generator's sequence.

The second method is the one most often used. Notice that the random integers are selected from the range 0 up to and including num-1 but not including num, which must be a positive integer.

QUESTION 5:

Say that you want pseudorandom numbers selected from the range 1 through 6. How can nextInt( int num ) be used for that?