简体   繁体   中英

How to Make two arrays that randomizes numbers between 10 and 30, and 11 and 30 in Java

I have to make two arrays that randomizes numbers between 10 and 30(first array), 11 and 30(second array) in java. i'm supposed to fill the first and second one in numerical order... how? i haven't started yet and i need to be finished soon.

int[] a = new int[20];


    int random = (int)(Math.random()*21+10);

    for(int x=0; x<a.length; x++){ 



        System.out.print(random + " ");
    }

    System.out.println();

    for(int i=11; i<=30; i++){
        System.out.print(i + " ");

    }

}

}

you can use

((Math.random() * ((max - min) + 1)) + min) to get random with limit

int min = 10;
        int max = 30;
        int[] a = new int[20];
        int[] b = new int[20];
        for(int i =0;i<a.length;i++) {
            a[i]= (int) ((Math.random() * ((max - min) + 1)) + min);
        }
        max =  31;
        for(int i =0;i<b.length;i++) {
            b[i]= (int) ((Math.random() * ((max - min) + 1)) + min);
        }

this will do

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM