Random numbers

Common

Math.random

double d = Math.random();

java.util.Random

Random random = new Random();
int i = random.nextInt();
random.ints(0, 1000).limit(7).forEach(System.out::println);
random.ints(7, 0, 1000).forEach(System.out::println);

ThreadLocalRandom

Random random = ThreadLocalRandom.current();
int randomNumber = random.nextInt();
int number = 1 + random.nextInt(10);
  • backed by Random class

Seed Value

  • this is used to generate a predicatable set of numbers for the random generators

Non predicatable

  • should use for cryptographic use cases ie passwords

  • java.security.SecureRandom

SecureRandom secureRandom = new SecureRandom();
double strongRandomNumber = secureRandom.nextDouble();
  • https://www.happycoders.eu/java/random-number/

Last updated

Was this helpful?