Generate 100 1-150 non-repeating numbers in Java

Source: Internet
Author: User

Tag:java   random    random number    

Import java.util.arrays;/** * Generates 100 1-150 digits in Java */public class Randomtest {public static void main (string[] args) {int[] Resultarr = Producenum (1, Num:resultarr, +); for (an Integer, "{") {System.out.println (num);} System.out.println ("+++++++++++++++++===================+++++++++++++");//Sort the result array and then output Arrays.sort (Resultarr); for (Integer Num:resultarr) {System.out.println (num);}} /** * Generates random numbers * @param minnum minimum number * @param maxnum Maximum number * @param numcount number of digits * @return result array */public static int[] Prod Ucenum (int minnum, int maxnum, int numcount) {//compared with his///if the number of random numbers is greater than the range that generates the random number, or the maximum number is less than the minimum number//return null directly, indicating that the entry does not meet the requirements if (Numcount ; (Maxnum-minnum + 1) | | Maxnum < Minnum) {return null;} The array that holds the result int[] Resultarr = new int[numcount];//count records the number of random numbers that have been generated int count = 0;while (Count < Numcount) {//generate random number int n UM = (int) (Math.random () * (maxnum-minnum)) + minnum;//flag defines whether the resulting random number is in the array boolean flag = true;//traversal of the generated random number for (i NT I=0; i<count; i++) {//the same as the number of random numbers generated by this time (num = = Resultarr[i]) {//If the same value already exists, jump out of the for Loop and continue the outer while loop, producing the next random number, flag = False;break;}} If the resulting random number does not exist in the array, the random number is stored in the array if (flag) {Resultarr[count] = num;//array has generated random numbers plus 1count++;}} return Resultarr;}}


Code interpretation

1 int num = (int) (Math.random () * (maxnum-minnum)) + Minnum;

Math.random () produces a random number of type double between 0~1

Math.random () * 10 will get a random number between 0 ~ 10, then int is 0 ~ 9, and if Math.random () * 10 + 1 is a random number between 1 ~ 11, then int is 1 ~ 10.

Here 1 corresponds to the minimum value in the program, and 11 is the maximum value.

This results in a random number ( int) (Math.random () * (maxnum-minnum)) + Minnum


2 For (int i=0; i<count; i++) {

Since num is randomly generated each time, there is no guarantee of duplicate problems, so only by going to the number in the existing array that has been generated to compare, the same, then re-produce, different, then add into the array.

Generate 100 1-150 non-repeating numbers in Java

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.