Use of Random classes in J2ME

xiaoxiao2021-03-06  17

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 designation The random random number, namely: NEXT (1) generated a random number NEXT (2) generated within 2 of 2, the random number of NEXT (3) generated 2 in 3 squares Random number ............................. Uxt (n) Generates the random number of random in 2 N times .. ............................... NEXT (32) Generates the random number of random in 2, which is equivalent to Nextint NEXTINT () used to generate random integers, up to 22, NEXTLONG () is used to generate random integers, up to 2 64 SESEED (long sees) to set the random number, that is, here SEED. 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. The example of a "wrong" written here: import java.util.Random; import javax.microedition.midlet *; public class J2ME extends MIDlet {private Random 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 ()} protected void destroyApp (Boolean Arg0 "Throws MidletStateChangeException {} The random number generated by this example is the same.

The correct approach should be the following modified Random = new random () in the protected void startapp () throws MIDletStateChangeException function (INT i = 0; i <20; i ) {system.out.println (random.nextint ));} The summary of the Random class is here, I believe that everyone can use this class correctly. Oh, please take care. MSN: Cuilichen@hotmail.com author blog:

http://blog.9cbs.net/cuilichen/

转载请注明原文地址:https://www.9cbs.com/read-50683.html

New Post(0)