-
random numbers
I know how to get a random number and set the seed. My problem herre is I have an array of 50 numbers and i want to give them all random numbers in a for loop or some simple way where i wouldn't have to write something for each one but I can't make them random enough.
-
>My problem herre is I have an array of 50 numbers and i want to give them all random numbers in a for loop
Is this what you mean? :confused:
Code:
int array[50];
for (int i=0; i<50; i++)
{
//Insert your code to generate a random number here
array[i] = number generated above;
}
//Print them out
for (int i=0; i<50; i++)
{
std::cout << array[i] << std::endl;
}
-
heres another way to do it... i dont know what your using but i use Turbo C so i'd have to include stdlib.h... but i dunno if you do to use the rand() function
Code:
int array[50];
int minrand=20; //minimum random number
int maxrand=50; //maximum random number
for (int i=0; i<50; i++)
{
array[i] = (rand()%(maxrand+1-minrand))+minrand;
}
does that make sense? lol... its an easier way to set the borders on the random number... i figured it might help >.<
-
If the numbers can be duplicated, then a simple loop would work fine. If the numbers should be unique, like randomly shuffling the numbers from 1-50 that are in the array, then the solution is to fill the array in order without using rand(), then use random_shuffle() to shuffle the array. If you cannot use something from <algorithm> to do the work for you, then you can use the same technique as random_shuffle uses to get the effect.