0

How can I generate 1000 random numbers between 1 and 5000 and insert those numbers to an array of size 500??

5th Aug 2016, 10:30 AM
Ahmed Shafeeu
Ahmed Shafeeu - avatar
14 Respostas
+ 1
Alright. change first array to 1000. Create a second array of 1500. after the first for loop, do another that is j < arr2.length. In that for loop, do arr2[j] = arr[Rand.nextInt(arr.length) + 1]; That should work. It'll randomly go through the array and add them to the other. There is the chance some numbers won't get put in, figure that's okay though?
5th Aug 2016, 11:54 AM
James
James - avatar
+ 1
Take what I told you in the first one but change array size to 1000, then do what I told you in the second.
5th Aug 2016, 12:00 PM
James
James - avatar
0
You can't do what you're describing. If you have an array of 500, that's all the array can hold. So if you have a 1000 random numbers, you'll either need more then one array, or set the array size to 1000
5th Aug 2016, 10:52 AM
James
James - avatar
0
What it's array size 1500
5th Aug 2016, 11:22 AM
Ahmed Shafeeu
Ahmed Shafeeu - avatar
0
You said 500. Anyhow. Create a random variable Random rand = new Random(); don't forget to import java.util.*; Create an array, int[] arr = new int[1500]; Create a for loop, for(int i = 0; i < 1000; i++) then, for each iteration, add to the array. arr[i] = rand.nextInt(5000) + 1; you do the + 1 so you don't get zero.
5th Aug 2016, 11:27 AM
James
James - avatar
0
I think if i use that code, All the elements after the 1000th term will be same... I mean rest of the 500 elements of the array will b 0 i guess
5th Aug 2016, 11:32 AM
Ahmed Shafeeu
Ahmed Shafeeu - avatar
0
Yup, they will be blank spaces. But that's what you asked for?
5th Aug 2016, 11:35 AM
James
James - avatar
0
No that's not what I wanted actually... I want to fill that array with those 1000 numbers... doesn't matter if some numbers are repeated also...
5th Aug 2016, 11:38 AM
Ahmed Shafeeu
Ahmed Shafeeu - avatar
0
You didnt ask that, read your question. Second, what you are asking makes no sense logically. You want to fill a 1500 array with 1000 random numbers? Why not just use 1500 random numbers? They're all between 1 and 5000, why is it 1000?
5th Aug 2016, 11:42 AM
James
James - avatar
0
cox repeating some numbers is necessary... If it is 1500 random numbers , then there is a chance that there will be no repetitive..
5th Aug 2016, 11:47 AM
Ahmed Shafeeu
Ahmed Shafeeu - avatar
0
Can you explain those 2 arrays... What is the main array there??
5th Aug 2016, 11:58 AM
Ahmed Shafeeu
Ahmed Shafeeu - avatar
0
Okay... I'll check it out... And thank you for your help... Btw is there any way by which I can contact you, like mail or contact number or fb??
5th Aug 2016, 12:03 PM
Ahmed Shafeeu
Ahmed Shafeeu - avatar
0
Don't give out personal info, but I frequent these forums daily.
5th Aug 2016, 12:04 PM
James
James - avatar
0
okay... thank you once again
5th Aug 2016, 12:07 PM
Ahmed Shafeeu
Ahmed Shafeeu - avatar