0

how to generate 6 random numbers in ascending order

here is my code i dont how to generate the 6 numbers and i already sorted it public static void main(String[] args) { int [] aryNums; aryNums = new int [52]; aryNums[0] = 1; aryNums[30] = 31; aryNums[1] = 2; aryNums[31] = 32; aryNums[2] = 3; aryNums[32] = 33; aryNums[3] = 4; aryNums[33] = 34; aryNums[4] = 5; aryNums[34] = 35; aryNums[5] = 6; aryNums[35] = 36; aryNums[6] = 7; aryNums[36] = 37; aryNums[7] = 8; aryNums[37] = 38; aryNums[8] = 9; aryNums[38] = 39; aryNums[9] = 10; aryNums[39] = 40; aryNums[10] = 11; aryNums[40] = 41; aryNums[11] = 12; aryNums[41] = 42; aryNums[12] = 13; aryNums[42] = 43; aryNums[13] = 14; aryNums[43] = 44; aryNums[14] = 15; aryNums[44] = 45; aryNums[15] = 16; aryNums[45] = 46; aryNums[16] = 17; aryNums[46] = 47; aryNums[17] = 18; aryNums[47] = 48; aryNums[18] = 19; aryNums[48] = 49; aryNums[19] = 20; aryNums[49] = 50; aryNums[20] = 21; aryNums[50] = 51; aryNums[21] = 22; aryNums[51] = 52; aryNums[22] = 23; aryNums[23] = 24; aryNums[24] = 25; aryNums[25] = 26; aryNums[26] = 27; aryNums[27] = 28; aryNums[28] = 29; aryNums[29] = 30; Arrays.sort(aryNums); int i; for (i=0; i<aryNums.length; i++) {System.out.println("winning number are" + aryNums[i]);} } }

29th Nov 2022, 2:30 PM
Nile lon
Nile lon - avatar
2 odpowiedzi
+ 3
Ohhh.. Are you not learned loops yet? Why don't you use loop there? for( int i = 0; i <= 5; i++ ) { aryNums[i] = i+1; } For only 6 random numbers, 52 elements array is not needed. For random number generation, you can a special class in java as Random ranObj = new Random(); //random object. ranObj.nextInt()*54; will give you a number between 0 to 54 edit: Brett Espiritu by using Math.random(): int[] aryNums = new int[5]; for( int i=0; i<5; i++) aryNums[i]= (int)(Math.random()*10); // generates random number between 0 to 10 Arrays.sort(aryNums); for(int i=0; i<aryNums.length; i++) System.out.println("winning number are: " + aryNums[i]); by using Random object : Random obj = new Random(); for( int i=0; i<5; i++) aryNums[i]=Math.abs( obj.nextInt()%10 ); // java.util.Arrays.sort(aryNums); for(int i=0; i<aryNums.length; i++) System.out.println("winning number are: " + aryNums[i]);
29th Nov 2022, 2:38 PM
Jayakrishna 🇮🇳
+ 1
1. Generate a random number for the first number. 2. Generate a random number and add it to the previous one for the next number. That way, it will be guaranteed to be higher than the previous one. 3. Repeat step 2 four more times.
30th Nov 2022, 2:05 AM
Brian
Brian - avatar