-
September 14th, 2012, 04:22 AM
#1
Random numbers
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.
-
September 14th, 2012, 04:31 AM
#2
Re: Random numbers
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.
-
September 14th, 2012, 02:12 PM
#3
Re: Random numbers
Put a srand(time()) on initialization.
-
September 15th, 2012, 06:29 AM
#4
Re: Random numbers
Originally Posted by laserlight
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.
-
September 15th, 2012, 06:33 AM
#5
Re: Random numbers
Originally Posted by ninja9578
Put a srand(time()) on initialization.
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.
-
September 15th, 2012, 06:47 AM
#6
Re: Random numbers
Call srand once. Only once at the start of your program.
-
September 15th, 2012, 08:41 AM
#7
Re: Random numbers
Originally Posted by GCDEF
Call srand once. Only once at the start of your program.
Means i should call srand outside the for loop?? How do i avoid having the zeros in the random number?? I just need the number among 1 to 5.
-
September 15th, 2012, 11:07 AM
#8
Re: Random numbers
Originally Posted by en061
Means i should call srand outside the for loop?
Before the loop, yes. One way is to call it as near as possible to the beginning of the global main function.
Originally Posted by en061
How do i avoid having the zeros in the random number?? I just need the number among 1 to 5.
With the current method that you are using, you can add 1 to the result to shift the range from [0, 4] to [1, 5].
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|