# Are random dates really random ### 22.5 Random numbers: Random, SecureRandom, SplittableRandom  The class provides a simple method for random numbers. Internally, however, it is based on a different class that we can also use directly, which gives us the option of not only asking random numbers between 0 and 1.

The usual random numbers in Java are so-called pseudo-random numbers because they are generated by a mathematical algorithm. Good "random values" only repeat themselves after very long sequences, have no obvious connection and are generated quickly. Perfect random numbers would never be predictable, the probability for each number would always be the same - regardless of the preceding values ​​- and the sequences would never repeat. ### 22.5.1The class random  The class in the package implements a pseudo random number generator. In contrast to, it does not offer any static functions, but a number of methods. The static function uses an object internally. ### 22.5.2 Build random objects with the seed  Each object requires a start value for the calculation. The starting value for each random number is a 48-bit seed. "Seed" is the English word for "seeds" and indicates that when random numbers are generated, like plants, there is a seed that leads to offspring. From this starting value, the random number generator then determines the following numbers using linear congruences. (As a result, the numbers are not really random, but obey a mathematical procedure.)

At the beginning there is a copy of the class. This instance is initialized with a random value (data type), which is then used for further calculations. This starting value shapes the entire sequence of generated random numbers, although it is not clear how the sequence behaves. But one thing is certain: two -objects generated with the same starting values ​​also deliver the same sequence of random numbers, that is, if the seed is the same, the sequence of random numbers is of course always the same; it's not that bad for tests. The parameterless standard constructor of initializes the start value with the sum of a magic start value and.

class java.util.Random

implements Serializable

• Generates a new random number generator.

• Generates a new random number generator and uses the parameter as a start value.

• Resets the seed. The generator then behaves in exactly the same way as a generator freshly created with this seed value. ### 22.5.3 Generate individual random numbers  The class generates random numbers for four different data types: (32-bit), (64-bit), and. There are four methods available for this:

• Returns the next pseudo-random number from the entire range of values, i.e. between and or and.

• Returns the next pseudo-random number between 0.0 and 1.0.

• Returns a pseudo-random number in the range from 0 to.

The class has a special method that can be used to generate a series of random numbers. This is the method. The parameter is a byte array and this is completely filled with random numbers.

Behind all methods for generating random numbers is the method. It is implemented in, but due to the visibility it is only visible from an inheriting class. Subclasses are possible because it is a completely normal, public, non-final class. ### 22.5.4 Pseudo-random numbers in the normal distribution *  Using a special method, we can obtain random numbers that satisfy a normal distribution:. This method works internally according to the so-called polar method and generates two normally distributed numbers from two independent pseudo-random numbers. The midpoint is 0 and the standard deviation is 1. The values ​​that are returned are -numbers and often near 0. Larger numbers are more likely to be rarer.

class java.util.Random

implements Serializable

• Returns the next random number in a Gaussian normal distribution with the center 0.0 and the standard deviation 1.0. ### 22.5.5 Generate stream of random numbers *  If several random numbers are required, a loop with repeated calls of is not necessary; instead there are two kinds of methods that deliver a bunch of random numbers.

First:

• Fills the array with random bytes.

Other methods provide a stream of random numbers:

[eg] example

Return ten random numbers, which are presumably prime numbers:

LongStream stream = new Random (). Longs (). Filter (v -> BigInteger.valueOf (v) .isProbablePrime (5));

stream.limit (10) .forEach (System.out :: println);

The methods, and are available in four varieties, see Table 22.13:

Parameterization

Explanation

Returns an infinite stream of random numbers in the complete range of values ​​of the primitives.

Returns a stream of random numbers.

Returns an infinite stream of random numbers with values ​​in the range from (inclusive) to (exclusive).

Returns a stream of random numbers with values ​​in the range from (inclusive) to (exclusive).

Table 22.13Stream methods of the random class

[eg] example

Output five floating point random numbers ranging from 10 to 20:

new Random (). doubles (5, 10, 20) .forEach (System.out :: println); ### 22.5.6 The SecureRandom class *  The class is caught in the dilemma of generating good and random numbers on the one hand, and being fast on the other. Cryptographically correct numbers do not generate, the implementation would have to drive more effort - which costs more time - and such good random numbers are not necessary in everyday life.

Cryptographically better random numbers are provided by a subclass of, the class. As a subclass, of course, it offers the same set of methods, only the quality of the random numbers is better. This is not due to itself, because the class does not implement any algorithms, but rather to the referenced, exchangeable random number providers. An implementation or the standard constructor provides, but then the object does not necessarily have to be "strong", i.e. at most conform to cryptographic standards. There is a difference, because SHA1PRNG returns and Windows-PRNG returns. ### 22.5.7SplittableRandom *  The class from the package has the task of supplying sequences of good random numbers. (Even if the class is called, it has nothing to do with you.) While the focus is more on the individual random number, the focus is on sequences of random numbers, which in particular include the Dieharder test  (http: // www. phy.duke.edu/~rgb/General/dieharder.php) exist.

The methods of therefore also revolve around streams of random numbers that are supplied as, and. There are also the well-known methods and a method that delivers a new one, so that two parallel threads continue to receive good random numbers independently.

How did you like the ? We always look forward to your friendly and critical feedback.

>> To the feedback form