In J2ME, because most of the games involve random events, the Random class naturally becomes a class that J2ME programmers often use. But for novices, the Random class seems simple, or there may be such errors. So here, I summarize me for this class.
The Random class is already defined in CLDC1.0. This class has two constructor Random () and Random (long sees) and provides four common methods:
Next (int bits) Nextint () NextLong () setseed (long seed)
Where NEXT (int bits) is used to generate a random number of the specified range, namely: NEXT (1) generates a random number NEXT (2) generated within 2 to generate two random numbers within 2 squares (3) ) The random number of random in 3 times is generated ........................... Uxt (n) generated 2 N times, the random number ... The random number is equivalent to nextint () NEXTINT () to generate random integers, the maximum 2 of 32 times next, to generate a random length, the maximum 2 64 SESEED (long seed) is used for Set the seeds of the random number, that is, the SEED herein. The use of random seeds is: Generally, the random class here is a pseudo-random number, which is generated by a specific algorithm. We can use a test to prove. The method is New two random class random1 and random2. Each call is 10 times, we can see that although each generated random number, the random number generated by the two random classes is the same. This makes the random number of vulnerabilities. If such a random number is used in a safe application, it will not achieve the expected effect. So the Random class provides this method to further increase randomness.
It is necessary to specifically, when we use the random number, we can't set a random class every time, no random seed, because the result is the random result. The number is the same. Write an example of "error" here: import java.util.random;
Import javax.microedition.midlet. *;
Public Class J2ME Extends Midlet {Private Random
Public J2ME () {}
Protected void startapp () throws midletStateChangeException {for (int i = 0; i <20; i ) {random = new random (); system.out.println (random.nextint ());}
}
protected void pauseapp () {}