Weekend, driving out to play with a girl friend, inside the car, random playing Jay’s songs. I am immersed in “get son float, get son float, get son meaning float” in, fancy oneself is autumn famous mountain car god, suddenly, the bean curd beside, oh no, girlfriend talk.










Pseudorandomness is a process that appears to be random, but is not. Pseudo-random number is a seemingly random periodic sequence, that is, regular randomness.






What is a random number

Random numbers are widely used in computer applications, and the most well-known is the application in cryptography. Random numbers have three characteristics, as follows:

Randomness: There is no statistical bias, is a completely chaotic sequence

Unpredictability: The inability to predict the next occurrence from a past sequence

Non-reproducibility: The same sequence cannot be reproduced unless the sequence itself is saved

How is the random play of music player implemented

Nowadays, music players are more intelligent, with some functions similar to song recommendation, which will randomly recommend songs to listeners. This kind of random recommendation is based on users’ listening habits, which is not within the scope of our discussion. We’re only talking about simple cases, such as using a simple player to randomly play a list of CD cards.

There are two common music Random playing algorithms, namely Random algorithm and Shuffle algorithm.

The Random algorithm

The Random algorithm is relatively simple. The next song is generated randomly only when the current song is played.

The Random algorithm performs a Random number operation when selecting the song to be played, and obtains the index of the song to be played in the playlist. The playlist itself is not disrupted, but just selects a song from the playlist for playing by using the Random function.

This Random Random number algorithm can be implemented using the Java language:

Calendar ca = Calendar.getInstance(); // Get the current system time int I; Random rand =new Random(ca.get(Calendar.MINUTE)*ca.get(Calendar.SECOND)); // Set the seed of the random number to minutes * seconds of the current system time I = rand.nextint (maxnum); //maxnum is the maximum value of a random numberCopy the code

Another defect of Random algorithm is that when “previous song” is clicked, it is exactly the same as the “next song” function. Random number is re-generated and selected from the playlist for playing instead of returning to the song just played.










Shuffle algorithm

The Shuffle algorithm is the opposite of the sorting algorithm. It is a process from order to disorder.

It scrambles the order of the songs in the playlist into an out-of-order playlist that has nothing to do with the original order, plays the songs later, and allows users to go back to the last song when they click on “Previous song”.

Compared with the Random algorithm, this algorithm is not Random in the complete sense, but disorderly ordering of the list of songs, and the playing order of songs is relatively fixed.

In Java, there is a ready-made implementation of the Shuffle algorithm, namely two overloaded shuffle methods in the Collections class:

public static void shuffle(List<? > list) { Random rnd = r;if(rnd == null) r = rnd = new Random(); shuffle(list, rnd); } private static Random r; public static void shuffle(List<? > list, Random rnd) { int size = list.size();if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
        for (int i=size; i>1; i--)
            swap(list, i-1, rnd.nextInt(i));
    } else {
        Object arr[] = list.toArray();

        // Shuffle array
        for (int i=size; i>1; i--)
            swap(arr, i-1, rnd.nextInt(i));

        // Dump array back into list
        ListIterator it = list.listIterator();
        for(int i=0; i<arr.length; i++) { it.next(); it.set(arr[i]); }}}Copy the code











True random and pseudo random

Random number is divided into true random number and pseudorandom number, our program use is basically pseudorandom number, which is divided into strong pseudorandom number and weak pseudorandom number.

  • Truly random numbers, obtained by physical experiments, such as coin rolling, dice, wheel spinning, electronic noise, nuclear fission, etc. Randomness, unpredictability, and unrepeatability need to be satisfied.

  • Pseudo random number, through a certain algorithm and seed. The software implements pseudo-random numbers.

    • Strong pseudorandom, hard to predict random number. Randomness and unpredictability need to be satisfied.

    • Weak pseudo-random number, easy to predict random number. Randomness needs to be satisfied.

When the Random algorithm and Shuffle algorithm are introduced above, the code implementation is pseudo-random algorithm. It can be said as follows:

As long as the random number is generated by a deterministic algorithm, it is pseudorandom. Only through constant algorithm optimization, make your random number more close to random.

Finite state machines cannot generate truly random numbers, so in modern computers, there is no way to generate truly random numbers through a pure algorithm. No matter what language it is, the numbers generated by a pure algorithm are pseudo-random numbers, which are generated by a seed from a determinable function.

This means that if you know the seed, you can infer information about the following random sequence of numbers. So there’s predictability.

So how do truly random numbers come about?

A truly random number is a random number derived from a truly random event.

Truly random numbers are generated using physical phenomena: coin rolls, dice, spinning wheels, electronic noise, nuclear fission, etc. Such random number generator is called physical random number generator, their disadvantage is relatively high technical requirements, low efficiency.

Existing true random number generators, such as PuTTYgen’s random number, allow the user to move the mouse up to a certain length, and then convert the mouse’s trajectory into a seed. Intel uses resistors and oscillators to generate thermal noise as information entropy resource. Dev /random and /dev/urandom for Unix/Linux use hardware noise to generate random numbers.

Therefore, to generate truly random numbers, it is impossible to use any pure algorithm to achieve. Both require the help of external physics.










Random number generator in Java

The Java language provides several Random number generators, such as the Random class mentioned earlier, and the SecureRandom class.

Pseudo-random number generator

Pseudorandom number generator uses a specific algorithm to convert a random number seed into a series of pseudorandom numbers. The pseudo-random number depends on the value of the seed; given the same seed value, the same random number is always generated. Pseudo-random number generation process only depends on CPU, does not rely on any external devices, the generation speed is fast, will not block.

Java provides the pseudo Random number generator with Java. Util. The Random class and Java. Util. Concurrent. ThreadLocalRandom class.

The Random class adopts AtomicLong to ensure the thread safety of multiple threads. However, as explained in the class annotation, the performance of concurrent Random number acquisition by multiple threads is poor.

ThreadLocalRandom can be used as a random number generator in multi-threaded environments. ThreadLocalRandom uses thread-local variables to improve performance, so long can be used instead of AtomicLong. ThreadLocalRandom is also byte populated to avoid pseudo-sharing.

Strong random number generator

Strong random number generators rely on random events provided by the underlying operating system. Strong random number generators are slow to initialize and generate, and may cause blocking due to the entropy accumulation required to generate sufficiently strong random numbers. Entropy accumulation usually comes from multiple sources of random events, such as the time interval between keystrokes, the distance and interval between mouse movements, and the time interval between specific interruptions. Therefore, use it only when you need to generate highly encrypted random data.

Java provides strong random number generator is Java. Security. SecureRandom class, the class is a thread-safe class, use the synchronize method to ensure the safety of the thread, but the JDK is not promised change SecureRandom thread safety in the future. Therefore, as with Random, performance problems may occur in a highly concurrent multi-threaded environment.

In Linux implementations, /dev/random and /dev/urandom can be used as random event sources. Because /dev/random is blocked, when reading random numbers, the entropy pool value is empty, which will block and affect the performance, especially when the system generates random numbers concurrently.

True random number generator

In Linux, SecureRandom is implemented with the help of /dev/random and /dev/urandom, which can generate random numbers using hardware noise.

http://random.org/ has been offering an online true random number service since 1998, which uses atmospheric noise to generate true random numbers. He also provides Java utility classes that you can use. Address: https://sourceforge.net/projects/randomjapi/










To avoid the problem, I dragged her back to the car and looped her favorite single from The Actor.