Use of the Random class in

Source: Internet
Author: User

In j2s, because most games involve Random events, the Random class naturally becomes a class that is frequently used by j2s programmers. However, for new users, the Random class seems simple and may encounter such errors. So here I will summarize my usage experience for this class.
The random class has been 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)
Here, next (int bits) is used to generate a random number within the specified range, namely:
Next (1) generates a random number within the power of 2
Next (2) generates a random number within 2's power
Next (3) generates a random number within the power of 2
...............................
Next (n) generates a random number within the power of N2.
...............................
Next (32) generates a random number within the power of 2, which is equivalent to nextInt ().
NextInt () is used to generate a random integer, up to 32 power of 2
NextLong () is used to generate random long integers, up to 64 power of 2
SetSeed (long seed) is used to set the seed of a random number. The use of Random number seeds is: Generally, the Random number generated by the Random class here is a pseudo-Random number, which is generated by the system using a specific algorithm. We can use a test to prove this. The methods are new random1 and random2 Random classes. Each calls the nextInt Method 10 times. We can see that although each generates a Random number, the Random numbers produced by the two Random classes are the same. This causes a random number vulnerability. If such a random number is used in a security application, the expected effect will not be reached. Therefore, the Random class provides this method to further improve randomness.
In this case, we need to point out that when we use Random numbers, we cannot add a new Random class every time without setting the Random number seed, because the result is, the random numbers generated each time are the same.
Write an "error" example here:
Import java. util. Random;
Import javax. microedition. midlet .*;
Public class j2_extends MIDlet {
Private Random random;
Public J2EE (){
}
Protected void startApp () throws MIDletStateChangeException {
For (int I = 0; I <20; I ++ ){
Random = new Random ();
System. out. println (random. nextInt ());
}
}
Protected void pauseApp (){

Related Article

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.