JAVA random number generation Int, Long, Float, Double

Random number Int generation

Generate borderless Int

@Test
public void testRandom_generatingIntegerUnbounded() throws Exception {

   int intUnbounded = new Random().nextInt();
   System.out.println(intUnbounded);
}

Generate bounded Int

@Test
public void testRandom_generatingIntegerBounded_withRange() throws Exception {

   int min = 1;
   int max = 10;
   int intBounded = min + ((int) (new Random().nextFloat() * (max - min)));
   System.out.println(intBounded);
}

Contains 1 but not 10

Use Apache Common Math to generate bounded Int

@Test
public void testRandom_generatingIntegerBounded_withApacheMath() throws Exception {

   int min = 1;
   int max = 10;
   int intBounded = new RandomDataGenerator().nextInt(min, max);
   System.out.println(intBounded);
}

Contains 1 and contains 10

Use Apache Common Lang's tool class to generate boundary Int

@Test
public void testRandom_generatingIntegerBounded_withApacheLangInclusive() throws Exception {

   int min = 1;
   int max = 10;
   int intBounded = RandomUtils.nextInt(min, max);
   System.out.println(intBounded);
}

Contains 1 but not 10

Use TreadLocalRandom to generate bounded Int

@Test
public void testRandom_generatingIntegerBounded_withThreadLocalRandom() throws Exception {

   int min = 1;
   int max = 10;
   int threadIntBound = ThreadLocalRandom.current().nextInt(min, max);
   System.out.println(threadIntBound);
}

Contains 1 but not 10


Random number Long generation

Create an unbounded Long

@Test
public void testRandom_generatingLongUnbounded() throws Exception {

   long unboundedLong = new Random().nextLong();
   System.out.println(unboundedLong);
}

Because the seed used by the Random class is 48 bits, nextLong cannot return all possible long values. Long is 64 bits.

Generate a bordered Long

@Test
public void testRandom_generatingLongBounded_withRange() throws Exception {

   long min = 1;
   long max = 10;
   long rangeLong = min + (((long) (new Random().nextDouble() * (max - min))));
   System.out.println(rangeLong);
}

The above will only generate random numbers of long type from 1 to 10

Use Apache Commons Math to generate bounded Long

@Test
public void testRandom_generatingLongBounded_withApacheMath() throws Exception {

   long min = 1;
   long max = 10;
   long rangeLong = new RandomDataGenerator().nextLong(min, max);
   System.out.println(rangeLong);
}

The RandomDataGenerator class mainly used in this method generates random numbers

Use Apache Commons Lang's tool classes to generate boundary Long

@Test
public void testRandom_generatingLongBounded_withApacheLangInclusive() throws Exception {

   long min = 1;
   long max = 10;
   long longBounded = RandomUtils.nextLong(min, max);
   System.out.println(longBounded);
}

RandomUtils provides a supplement to java.util.Random

Use ThreadLocalRandom to generate a bounded Long

@Test
public void testRandom_generatingLongBounded_withThreadLocalRandom() throws Exception {

   long min = 1;
   long max = 10;
   long threadLongBound = ThreadLocalRandom.current().nextLong(min, max);
   System.out.println(threadLongBound);
}


Random number Float generation

Generate Float random numbers between 0.0-1.0

@Test
public void testRandom_generatingFloat0To1() throws Exception {

   float floatUnbounded = new Random().nextFloat();
   System.out.println(floatUnbounded);
}

The above will only generate float type random numbers containing 0.0 but not 1.0

Generate bounded Float random numbers

@Test
public void testRandom_generatingFloatBounded_withRange() throws Exception {

   float min = 1f;
   float max = 10f;
   float floatBounded = min + new Random().nextFloat() * (max - min);
   System.out.println(floatBounded);
}

Use Apache Common Math to generate bounded Float random numbers

@Test
public void testRandom_generatingFloatBounded_withApacheMath() throws Exception {

   float min = 1f;
   float max = 10f;
   float randomFloat = new RandomDataGenerator().getRandomGenerator().nextFloat();
   float generatedFloat = min + randomFloat * (max - min);
   System.out.println(generatedFloat);
}

Use Apache Common Lang to generate bounded Float random numbers

@Test
public void testRandom_generatingFloatBounded_withApacheLang() throws Exception {

   float min = 1f;
   float max = 10f;
   float generatedFloat = RandomUtils.nextFloat(min, max);
   System.out.println(generatedFloat);
}

Use ThreadLocalRandom to generate bounded Float random numbers

ThreadLocalRandom class not provided


Random number Double generation

Generate a Double random number between 0.0d-1.0d

@Test
public void testRandom_generatingDouble0To1() throws Exception {

   double generatorDouble = new Random().nextDouble();
   System.out.println(generatorDouble);
}

Same as Float, the above method will only generate random numbers containing 0.0d but not 1.0d

Generate Double random numbers with bounds

@Test
public void testRandom_generatingDoubleBounded_withRange() throws Exception {

   double min = 1.0;
   double max = 10.0;
   double boundedDouble = min + new Random().nextDouble() * (max - min);
   System.out.println(boundedDouble);
   assertThat(boundedDouble, greaterThan(min));
   assertThat(boundedDouble, lessThan(max));
}

Use Apache Common Math to generate bounded Double random numbers

@Test
public void testRandom_generatingDoubleBounded_withApacheMath() throws Exception {

   double min = 1.0;
   double max = 10.0;
   double boundedDouble = new RandomDataGenerator().getRandomGenerator().nextDouble();
   double generatorDouble = min + boundedDouble * (max - min);
   System.out.println(generatorDouble);
   assertThat(generatorDouble, greaterThan(min));
   assertThat(generatorDouble, lessThan(max));
}

Use Apache Common Lang to generate bounded Double random numbers

@Test
public void testRandom_generatingDoubleBounded_withApacheLang() throws Exception {

   double min = 1.0;
   double max = 10.0;
   double generatedDouble = RandomUtils.nextDouble(min, max);
   System.out.println(generatedDouble);
}

Use ThreadLocalRandom to generate bounded Double random numbers

@Test
public void testRandom_generatingDoubleBounded_withThreadLocalRandom() throws Exception {

   double min = 1.0;
   double max = 10.0;
   double generatedDouble = ThreadLocalRandom.current().nextDouble(min, max);
   System.out.println(generatedDouble);
}

How many classes or methods can implement random numbers in JAVA?

  • The java.util.Random class provides methods to generate random numbers of Bytes, Int, Long, Float, Double, and Boolean
  • The java.util.Math.random method provides a method to generate Double random numbers. The internal implementation of this method also calls the nextDouble method of java.util.Random, but it provides better support for multi-threading. When threads are concurrent, it will reduce the competition of each random number generator
  • Third-party tools, such as the random number generation classes provided in the Apache Common Lang library and the Apache Common Math library, really use one line of code to achieve complex random number generation
  • java.util.concurrent.ThreadLocalRandom is a random number generator designed for multi-threaded concurrent use. The method used is ThreadLocalRandom.current.nextInt(). This class is provided in JDK1.7 and is especially suitable for the ForkJoinTask framework. This class directly provides the operation of generating bounded random numbers, for example public int nextInt(int origin, int bound), it can also realize complex random number generation with one line of code.

The final summary is that the java.util.Random class is used in a single thread, and the java.util.concurrent.ThreadLocalRandom class is used in a multi-thread.

Guess you like

Origin blog.csdn.net/qq_41988225/article/details/106720537