There are three ways to do this:
- Math.random (): This method returns a random double type of [0.0, 1.0]. It is actually called the Nextdouble () method of the Random class. But the math class uses a static random number generator (new random ()), which is a thread-safe method, so multiple threads share a random number generator. If many threads are using the random number generator frequently, it is better to assign a random number generator to each thread. In addition, this method does not seem to be supported in J2ME.
- The Random class: a powerful pseudo-random number generator provided by Java. There are two methods for constructing methods and the main six classes to get random numbers.
- Two construction methods:
- random (): Using System Time (System.nanotime (), nanosecond level) as a seed, this random number generator generates random numbers that are always different
- random (Long Seed): Using a specific seed, if the two random number generators have the same seed, the same method is called in the same order, and they return the same random number. (In other words, the pseudo-random number is given an initial value, and then a calculation, the next one, the next value.) So once the seed is determined, then all the random number sequences are actually deterministic. So it's called a pseudo-random number. Online poker can sometimes cheat by synchronizing with the server system clock to get the same clock seed, so that it can determine the sequence of random numbers and know all the cards.
- class six Gets the random number method: Nextint (), nextdouble (), Nextlong (), Nextboolean (), Nextfloat (), Nextgaussian (). Of course, nextint additional gauge, can be achieved through the nextint (), do not say ~
- System.currenttimemillis: This returns the number of milliseconds from January 1, 1970 0:0 0 seconds to the current one. After this time, you can get the random number by taking a modulo (this is actually the generation method of defining the random number, which is what the random class does). Of course, the second type of random constructor can be used as a seed, so the random number of each return is different, because the time of the call must be different.
|