Random digital processing in J2ME
Author: Chen Yuefeng
From: http://blog.9cbs.neet/mailbomb
Generate random numbers in the program, compare, such as artificial intelligence areas, etc., here is a simple finishing for the operation of generating random numbers in J2ME, I hope to help everyone.
J2ME and J2se are different, and the random numbers cannot be generated using the random number of Random using the Math class.
1. Create an object of the Random type:
Random Random = new random ();
Random Random = New Random (10010010);
The above two are the way to create a Random object, the first use of the default constructor, and the following code effect is equivalent:
Random Random (SYSTEM. CURRENTTIMEMILLIS ());
It is quite created with the current time as a seed number.
The second way is created by specifying seed numbers by ourselves.
Everyone can use any of the above two ways as needed.
2, generate random numbers:
After creating a random object, we can generate random numbers:
Generate random integers:
INT K = random.nextint ();
Generate random integers:
Long L = random.nextlong ();
3, generate the number of the specified range:
For example, a random number between 0-10:
INT K = random.nextint ();
INT j = math.abs (k% 10);
First, a random integer K is generated, then by K and 10, and finally the absolute value is taken by the ABS method of the MATH class, and the random number between 0-10 is obtained.
Get the random number between 0-15, Similar:
INT K = random.nextint ();
INT j = math.abs (k% 15);
Get random numbers between 10-20:
INT K = random.nextint ();
INT j = math.abs (k% 10) 10;