"Java" produces different random numbers within a certain range

Source: Internet
Author: User

First of all, it is simple to generate a random number in Java, not like other programming languages such as c,javascript,asp, which will only produce a random decimal between 0-1 and then let you calculate by some mathematical formulas. The ability to produce a random number as easily as you want. Remember to introduce java.util.* on the line.

For example, the following programs:

Import java.util.*;p Ublic class Random_test {public static void main (string[] args) {System.out.println (New Random (). Nextdouble ());//can produce a 0-1 random decimal System.out.println (new Random (). Nextint (10));//can produce a random integer greater than or equal to 0, less than but not equal to 10, that is, single-digit}}

However, in order to produce different random numbers in a certain range, it is not so easy to show a random number of patterns.

Before I in the "ASP" in a specific range to produce n different random number (click to open the link) Once discussed this problem, but unfortunately ASP inside the code mix, that is, VBScript, often mixed with HTML code, can only give the corresponding algorithm, The following is a description of the program using specific Java executable code:

Import Java.util.*;import java.io.*;p ublic class Random_different {public static void main (string[] args) throws Ioexcept Ion{system.out.print ("input generated random number range, 1 to n,n="), int n=0;try{n=integer.parseint (new BufferedReader (New InputStreamReader (system.in)). ReadLine ());} catch (Exception e) {System.out.println ("n must be a positive integer"); return;} if (n<1) {System.out.println ("n must be a positive number"); return;} int randarr[]=new int[n];int I=0;while (i<n) {int rand= (new Random (). Nextint (n) +1); Boolean israndexist=false;for ( int j=0;j<randarr.length;j++) {if (Randarr[j]==rand) {israndexist=true;break;}} if (israndexist==false) {randarr[i]=rand;i++;}} System.out.println (arrays.tostring (Randarr));}}

Thought or the thought of the last time:

First, a random number x is generated in the 1~n, and then the previously generated data is traversed to determine if there is a number equal to the newly generated random number, and if so, the flag,

Then, after the previous data has been traversed, the flag is judged to stand up,

If it is, do not add this random number into the array, re-generate the random number and the flag, and then re-traverse the existing data whether the random number already exists,

If no, add until n data is finished.

Finally, the output of this N data.

The specific operating results are as follows:


In order to illustrate that this program is robust, let the computer enter N, where this n from 1-20, can be observed, the output of the array does not have a number is the same, complete the task!


More number is also no problem, you set n= 200,000, there is no problem, here due to the monkey machine is too slag, set a n=33 to illustrate the effect.


"Java" produces different random numbers within a certain range

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.