Java – Random class

Random class generates pseudo-random numbers, not really random numbers but they appear to be.

use:

import java.util.Random;
...
Random rand = new Random();
int randomNumber = rand.nextInt();

common methods:

  • nextInt() – Returns the next pseudorandom, uniformly distributed int value from this random number generator’s sequence.
  • nextInt(max) – Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator’s sequence.
  • nextDouble() – Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator’s sequence.

Read more here.

Java random class, <span>Java – Random class</span>, KandZ
Java random class, <span>Java – Random class</span>, KandZ

Random Number Within a Given Range

Java random class, <span>Java – Random class</span>, KandZ
Java random class, <span>Java – Random class</span>, KandZ

Random letter

Random rand = new Random();
char letter = (char)('A' + rand.nextInt(26));
char letterS = (char)('a' + rand.nextInt(26));
System.out.println(letter);
System.out.println(letterS);

THE END!!!

86960cookie-checkJava – Random classJava – Random class

Related posts

2 Thoughts to “Java – Random class

  1. […] Java – Random class […]

Comments are closed.