0
How can I generate 1000 random numbers between 1 and 5000 and insert those numbers to an array of size 500??
14 Réponses
+ 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?
+ 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.
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
0
What it's array size 1500
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.
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
0
Yup, they will be blank spaces. But that's what you asked for?
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...
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?
0
cox repeating some numbers is necessary... If it is 1500 random numbers , then there is a chance that there will be no repetitive..
0
Can you explain those 2 arrays... What is the main array there??
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??
0
Don't give out personal info, but I frequent these forums daily.
0
okay... thank you once again