Java random number Reandom (simple introduction)

Source: Internet
Author: User
Tags generator

Simple Introduction

There are two kinds of random functions in Java

First, Java.lang.Math.Random;

Call this math.random () function to return a double with a positive sign, which is greater than or equal to 0.0 and less than 1.0, that is, the value range is [0.0,1.0] the left closed right open interval, the return value is a pseudo-random selection of the number, within that range (approximate) evenly distributed.

Second, Java.util.Random

Two ways to construct Random ():

1. Random (): Creates a new random number generator.

2. Random (Long Seed): Creates a new random number generator with a single long seed.

We can specify the seed when constructing the Random object (specify what the seed does here, and then look down)

For example: random r1 = new Random (20), or the number of milliseconds of the default current system time as a seed number: random r1 = new Random ();

It is necessary to note that when you create a random object you can give any number of valid seed numbers, the seed number is only the origin number of the random algorithm, and the interval of the generated random number has no relation.

Java generates a random number that requires 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.

6, public double nextdouble () The method is to produce a double type random number.

7, public synchronized double Nextgoussian () The method is to produce a double type of Goussian random number.

Case LIST:

 PackageCOM.WBG;Importjava.lang.*;ImportJava.util.Random; Public classRandomapp { Public Static voidMain (String args[]) {Random ran1=NewRandom (); Random ran2=NewRandom (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         for(inti = 0; I < 5; i++) {System.out.println (Ran2.nextint ()+ " "); if(i = = 2) System.out.println ();//produces different random numbers of the same type.         }    }}
View Code

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

Say you want a random number between 0-10.

You can write this (int) (Math.random () *10);

Java generates a specified range of random number generation mechanism: the generation of Min-max between the digital implementation principle:

cannot be set to int, must be set to long

Math.Round (Math.random () * (max-min) +min) long Temp;

Generate random numbers from 1000 to 9999

Temp=math.round (Math.random () *8999+1000);

Summarize:
// The first argument is the ending number, the second argument is the starting number.  Public Static int reandom (int stop,int  stat) {        return (int) Math.Round (Math.random () * (stat-stop) +stop);    }

Java random number Reandom (simple introduction)

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.