A few tricks to implement Java random numbers

Source: Internet
Author: User
Tags time in milliseconds


One, in Java.util this package is provided with a random class, we can create a random object to generate a stochastic number, you can generate random integers, random float, random double, random long, this is also our regular use of a
The method of taking the random number.

Two, there is a Currenttimemillis () method in our system class, this method returns a number of milliseconds from January 1, 1970 0:0 0 seconds to the current one, the return type is long, we can take him as a random number,
We can take him to a number of models, we can limit him to a range of

Third, in fact, in the default constructor of the random method is also used in the above method to generate random numbers


The following instructions are available for the random class in method two:

the Java.util.Random class is built in two ways: with seeds and without seeds

Without seeds:
This method will return a random number,each run results in a different

public class Randomtest {
public static void Main (string[] args) {
Java.util.Random r=new java.util.Random ();
for (int i=0;i<10;i++) {
System.out.println (R.nextint ());
}

}
with seeds:
this way, no matter how many times the program runs, the result is the same

public static void Main (string[] args) {
Java.util.Random r=new Java.util.Random (10);
for (int i=0;i<10;i++) {
System.out.println (R.nextint ());
}
}

The difference between the two approaches is that

(1) First Open the Java Doc, and we'll see a description of the Random class:

An instance of this class is used to generate a pseudo-random number stream, which uses a 48-bit seed that can be modified with a linear congruence formula (see Donald Knuth's "The Art of computer programming, Volume 2", section 3.2.1).

If you create two Random instances with the same seed, the same method call sequence is made for each instance, and they will generate and return the same sequence of numbers. To ensure this, we specify a specific algorithm for the class random. For the full portability of Java code, the Java implementation must have the class Random use all the algorithms shown here. However, subclasses of the Random class are allowed to use other algorithms as long as they conform to the general contract of all methods.

Java Doc has explained the random class very well, and our tests have verified this.

(2) If no seed number is provided, the seed number of the random instance will be the number of milliseconds of the current time, which can be obtained by System.currenttimemillis () to obtain the current time in milliseconds. Open the source code of the JDK and we can see it very clearly.

/**
* Creates a new random number generator. Its seed was initialized to
* A value based on the current time:
* Random () {This (System.currenttimemillis ());} Java.lang.system#currenttimemillis ()
*/
Public Random () {This (System.currenttimemillis ());}


Other than that:

Description of the Nextint (), nextint (int n) method of the Random object:

int Nextint ()
Returns the next pseudo-random number, which is an int value that is evenly distributed in the sequence of this random number generator.
int nextint (int n)
Returns a pseudo-random number, which is an int value that is taken out of the sequence of this random number generator, evenly distributed between 0 (inclusive) and a specified value (not included).



30 students study number 20171118 to 20171148 all elective Java programming courses, all students score randomly generated 60-100 random number
 Packageltb6w;ImportJava.util.Random; Public classStuscore {Random R=NewRandom (); Private intNum=0; Private intSum=0; Private intstudentid=20171118; PrivateString java= "Java Programming Course:";  PublicStuscore () {num= This. Getrandom (); }                                  Public voidGetscore () { while(sum<31) {                                            if(num>100| | Num<60) {num= This. Getrandom (); Continue; } System.out.println ("Study Number:" +studentid+ "" +java+ "Score:" +num); Sum++; StudentID++;  This. Getrandom (); }            }                 Public intgetrandom () {num=r.nextint (100); returnnum; }             Public Static voidMain (string[] args) {Stuscore St=NewStuscore ();            St.getscore (); }}

A few tricks to implement Java random numbers

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.