Random number of confusion (java.util.randommath.random ()

xiaoxiao2021-03-06  42

Green:

I want to generate a random submarine appearance in J2ME, in fact, only one Y coordinate is needed. With the nextint, the code is similar to:,

The strange thing is to get two random numbers, but it is two identical numbers: on the spot

Class T {

INT X;

Java.util.random r = new java.util.random ();

T () {

x = (R.NextINT () >>> 1)% 10; // Generate a number between 0-9,

}

INT gett () {

Return this.x; // Get this random number

}

}

Public Class Tr {

Public static void main (String [] args) {

For (int i = 0; i <2; i ) {

T t = new t ();

System.out.println (T. Gett ()); // Output this random number, but the result is large,

}

}

}

So some information about random numbers:

The conclusion is as follows:

1. The same reason is that the random number is that the random number is the current time to do a random seed, that is, new random (time is seed), so the program will appear as the same reason.

Solution: Let him have a period of time when generating random numbers, such as:

Try {thread.sleep (100);} catch (exception e) {}

There is no problem with it.

About random: Method for generating random:

1. Call nextint () after random () to generate random numbers, as for the scope of you want to generate yourself, you can do it.

Such as: (Rand.Nextint () >>> 1)% 101

2. In fact, it is already very old with nextint (), and if it is generally used as a random number of random numbers (with the above method, taking the normal molding), the value is small, so the version of JDK upgrade The usage is the random number between NEXTINT (100) 0-100:

3. In fact, there is also a way to be math.random () to generate a floating point between 0-1: You can * you want to range, fetch, this method can effectively avoid the problem of repetition above,

4. Usage on your phone: Unfortunately, J2ME only provides nextint () does not provide nextint (range) (MIDP2.0 is not supported), do not support math.random, I want to use floating point operations, I want to mobile phone or I can't use it, (I don't know why use the nextint WTK can run, (JBuilderx prompts this method? But there is no in the API, if you use S60 to make Device, this place will report error, other Device will not report error,) But on the 6600 of the Chicken, not, huh, huh, I have already found this problem, the chicken is not reported on the chicken, cold ~~~)

to sum up:

Generally, it is best to use Nextint. If it is not a lot of calculation, you can use Math.random, (because he can run slowly, fetching after floating point), like I have no way, I have to use the first Method

Confusion: The random number generated by the following method is not too repeated, what solutions ~~~

Public Class Tr {

Public static void main (string [] args) {java.util.random r = new java.util.random ();

INT X;

For (int i = 0; i <2; i ) {

X = (r.nextint () >>> 1)% 101;

System.out.println (x);

}

}

}

Reference article:

http://www.9cbs.com.cn/program/2848.htm

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

New Post(0)