I want to have the random numbers between 1 to 5 for a variable of 5arrays. for having the random numbers i am using the code as below.
Code:
for(int i=1;i<=5;i++)
{ int j=rand()%5;
cout<<j<<endl;
}
The problem with this way of generating random number is that i get the same set of random numbers in every outputs. Is there any other way to have different random numbers with many different outcomes as much as possible.
You probably forgot to use srand to seed the PRNG.
Also, if the numbers must be different on each iteration, then you would be better off creating an array of integers from 1 to 5 then shuffling the array with std::random_shuffle.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
You probably forgot to use srand to seed the PRNG.
Also, if the numbers must be different on each iteration, then you would be better off creating an array of integers from 1 to 5 then shuffling the array with std::random_shuffle.
Please explain me a bit more about it...i am really getting so confused..i am just a beginner in C++ Its only been few months that i am trying to learn it.
for (int j=1;j<=5;j++)
{
for (int i=1;i<=5;i++)
{
srand (i);
printf ("First number: %d\n", rand() % 5);
}
printf ("\n");
}
Is this the way i should do? In this output also every time i am getting the same sets of random numbers...The output is always
1 0 3 1 4
1 0 3 1 4
1 0 3 1 4
1 0 3 1 4
1 0 3 1 4
What should i be doing to get outputs like..
1 2 4 3 4
3 2 3 1 3
2 3 4 5 2
etc...differents numbers between 1 and 5 very randomly..i am just a begginer please do suggest me some easy and efficient method of doing it.
Bookmarks