Java get random number

Source: Internet
Author: User

In Java we can use the Java.util.Random class to produce a random number generator. It has two forms of constructors, each of which is random () and random (long Seed). Random () uses the current time, System.currenttimemillis () as the seed of the generator, and random (long Seed) uses the specified seed as the seed of the generator.

After the random number generator (randomness) object is produced, different types of random numbers are obtained by calling different Method:nextint (), Nextlong (), Nextfloat (), nextdouble (), and so on.

1> Generating Random numbers
Random random = new random ();
Random random = new random (100);//Specify seed number 100
Random calls a different method to get the stochastic number.
Assuming that 2 random objects use the same seed (for example, 100) and call the same function in the same order, the return value is exactly the same. The output of the two random objects in the following code is completely identical
Import java.util.*;
Class Testrandom {
public static void Main (string[] args) {
Random random1 = new random (100);
System.out.println (Random1.nextint ());
System.out.println (Random1.nextfloat ());
System.out.println (Random1.nextboolean ());
Random random2 = new random (100);
System.out.println (Random2.nextint ());
System.out.println (Random2.nextfloat ());
System.out.println (Random2.nextboolean ());
}
}

2> a random number within a specified range
Random number control within a range, using the modulo operator%
Import java.util.*;
Class Testrandom {
public static void Main (string[] args) {
Random random = new random ();
for (int i = 0; i < 10;i++) {
System.out.println (Math.Abs (Random.nextint ())%10);
}
}
}
The obtained random number has positive negative, using math.abs to make the obtained data range non-negative

3> gets the non-repeating random number within the specified range
Import java.util.*;
Class Testrandom {
public static void Main (string[] args) {
int[] intret = new Int[6];
int INTRD = 0; Store random Numbers
int count = 0; Record the number of random numbers generated
int flag = 0; Whether a flag has been generated
while (count<6) {
Random RDM = new Random (System.currenttimemillis ());
INTRD = Math.Abs (Rdm.nextint ())%32+1;
for (int i=0;i<count;i++) {
if (INTRET[I]==INTRD) {
flag = 1;
Break
}else{
Flag = 0;
}
}
if (flag==0) {
Intret[count] = INTRD;
count++;
}
}
for (int t=0;t<6;t++) {
System.out.println (t+ ", +intret[t]);
}
}
}

Java random number class introduction

Class Java.util.Random in the Java Useful Tools class Library provides methods for generating random numbers of various types. It can produce random numbers of types such as int, long, float, double, and Goussian. This is also the biggest difference between it and the method in Java.lang.Math, which only produces a random number of type double.
The method in class random is very simple, it has only two construction methods and six common methods.
Construction Method:
(1) public Random ()
(2) Public Random (long Seed)
Java generates a random number with a base value of seed, and in the first method the base value defaults to the system time as seed.
Common methods:
(1) Public synonronized void Setseed (Long Seed)
The method is to set the base value seed.
(2) public int nextint ()
The method is to produce an integer random number.
(3) public long Nextlong ()
The method is to produce a long type random number.
(4) Public float nextfloat ()
The method is to produce a float-type random number.
(5) public double nextdouble ()
The method is to produce a double type random number.
(6) Public synchronized double Nextgoussian ()
The method is to produce a double type of Goussian random number.
Example 2 Randomapp.java.
Import java.lang.*;
Import Java.util.Random;

public class randomapp{
public static void Main (String args[]) {
Random ran1=new random ();
Random ran2=new random (12345);
Two classes of random objects were created.
System.out.println ("The 1st set of random numbers:");
System.out.println ("Integer:" +ran1.nextint ());
System.out.println ("Long:" +ran1.nextlong ());
System.out.println ("Float:" +ran1.nextfloat ());
System.out.println ("Double:" +ran1.nextdouble ());
System.out.println ("Gaussian:" +ran1.nextgaussian ());
Generate various types of random numbers
System.out.print ("The 2nd set of random numbers:");
for (int i=0;i<5;i++) {
System.out.println (Ran2.nextint () + "");
if (i==2) System.out.println ();
produces different random numbers of the same type.
System.out.println ();
}
}
}

Random random=new random ();
Random.nextint ();

can also have nextfloat and so on, all kinds of basic types have

Math.random can also
Let's say you want a random number between 0-10.
You can write like this.
(int) (Math.random () *10);

Java produces a specified range of random numbers

Java generates a specified range of random numbers
Production mechanism:
Generate a number between Min-max
Implementation principle:
Math.Round (Math.random () * (max-min) +min)

Long Temp; cannot be set to int, must be set to long
Generate random numbers from 1000 to 9999
Temp=math.round (Math.random () *8999+1000);

Java get random number

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.