How Java generates random numbers

Source: Internet
Author: User
Tags random shuffle

First, how does Java generate random numbers?

1. Open Eclipse

2. Create a new Java project, for example, named "Suijishu" click Finish

3. Create a new class to test

4. First insert a package in the head

Enter import Java.util.Random;

5. Enter the code:

public static int Suiji = new Random (). Nextint (10);

public static void Main (string[] args) {

System.out.print (Suiji);

}

where public static int Suiji = new Random (). Nextint (10); This line is the key to generating a random number, which can generate a random number from 0 to 9

 Package Suijishu; Import Java.util.Random;  Public class suijishu{    publicstaticint Suiji =new Random (). Nextint (10 );      Public Static void Main (string[] args) {        System.out.print (Suiji);    }}

6. Test the output, you can see the generated random number

Ii. how Java implements generating non-repeating random numbers

 PackageSuijishu;ImportJava.util.Random; Public classsuijishu{ Public Static intSuiji =NewRandom (). Nextint (10);  Public Static voidMain (string[] args) {System.out.print (Suiji); }           //set several numbers           Private Static Final intLENGTH = 8; /*** This is a typical random shuffle algorithm. * The process is to select one from the alternate array into the target array, remove the selected array from the alternate array (put to the last, and narrow the selection) * Algorithm time complexity O (n) *@returnRandom 8 is a non-repeating array*/            Public Staticstring Generatenumber () {string No= ""; //initializing an alternate array               int[] Defaultnums =New int[10];  for(inti = 0; i < defaultnums.length; i++) {Defaultnums[i]=i; } Random Random=NewRandom (); int[] Nums =New int[LENGTH]; //the part length that can be selected in the default array               intcanbeused = 10; //populating the target array                for(inti = 0; i < nums.length; i++) {                   //storing randomly selected numbers in the target array                   intindex =Random.nextint (canbeused); Nums[i]=Defaultnums[index]; //throws the used number to the alternate array at the end and reduces the optional areaSwap (index, canbeused-1, defaultnums); Canbeused--; }               if(Nums.length > 0) {                    for(inti = 0; i < nums.length; i++) {No+=Nums[i]; }               }               returnNo; }           /*** Exchange Method *@paramI exchange position *@paramJ Position of Interchange *@paramnums Array*/           Private Static voidSwapintIintJint[] nums) {               inttemp =Nums[i]; Nums[i]=Nums[j]; NUMS[J]=temp; }           /*** Get 8 digits *@return            */            Public Staticstring GenerateNumber2 () {string No= ""; intNum[] =New int[8]; intc = 0;  for(inti = 0; I < 8; i++) {Num[i]=NewRandom (). Nextint (10); C=Num[i];  for(intj = 0; J < I; J + +) {                       if(Num[j] = =c) {i--;  Break; }                   }               }               if(Num.length > 0) {                    for(inti = 0; i < num.length; i++) {No+=Num[i]; }               }               returnNo; }}

The test output will find that every random number generated is different.

How Java generates random numbers

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.