The use of random class in J2ME

Source: Internet
Author: User

In J2ME, since most games involve random events, the random class naturally becomes a class that J2ME programmers often use. But for beginners, the random class seems simple, and it is possible that such a mistake can occur. So here's a summary of my experience with this class.

The random class is already defined in CLDC1.0. This class has two constructors random () and random (long Seed) and provides four common methods:

Next (int bits)
Nextint ()
Nextlong ()
Setseed (Long Seed)

where next (int bits) is used to produce a random number in the specified range, namely:
Next (1) produces a random number within 2 of the 1-second square
Next (2) produces a random number within 2 of the 2-second square
Next (3) produces a random number within 2 of the 3-second square
...............................
Next (n) produces a random number within the N-second square of 2
...............................
Next (32) produces a random number within the 32-second square of 2, which is equivalent to Nextint ().
Nextint () is used to produce random integers, with a maximum of 2 of the 32 sides
Nextlong () is used to produce random long integers, with a maximum of 2 of the 64 sides
Setseed (long Seed) is used to set the seed of a random number, that is, seed here. The use of random number seeds is: Generally speaking, the random class here produces random numbers is pseudo-random number, is the system using a specific algorithm generated, on this we can use a test to prove. The method is new two random class random1 and random2. Each call to the Nextint method 10 times, we can see that, although each produced a random number, but two random class generated random numbers are the same. This makes the random number a loophole. If such a random number is used on a security application, it will not achieve the desired effect. So the random class provides this method to further improve randomness.

What needs to be pointed out here is that when we use random numbers, we can't have a new random class and don't set random number seeds, because the result is that every random number is the same.
Write an example of an "error" 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 < i++) {
Random=new random ();
System.out.println (Random.nextint ());
}

}

protected void Pauseapp () {
}

protected void Destroyapp (Boolean arg0) throws Midletstatechangeexception {
}
Many of the random numbers produced by this example are the same. The correct approach should be to make the following modifications in the protected void startApp () throws Midletstatechangeexception function
Random=new random ();
for (int i = 0; i < i++) {
System.out.println (Random.nextint ());
}
The summary of the random class is here, I believe we can use this class correctly. Oh, please care more.
Msn:cuilichen@hotmail.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.