I am trying to generate two sets of 8 random numbers between (0 through 7) such that:
1. none of the 8 number repeats
2. the 8 numbers can repeat
Printable View
I am trying to generate two sets of 8 random numbers between (0 through 7) such that:
1. none of the 8 number repeats
2. the 8 numbers can repeat
For number 1, all you need is to initialize an array with 8 elements numbered 0,1,2,3,4,5,6,7. You then call the std::random_shuffle() function on the array. This shuffles the data around using a random number generator (you don't even have to supply one). The std::random_shuffle function is an STL algorithm that can be found in the <algorithm> header file.Quote:
Originally posted by doumalc++
I am trying to generate two sets of 8 random numbers between (0 through 7) such that:
1. none of the 8 number repeats
2. the 8 numbers can repeat
For the second, you initialize an array but for each element, the value will be the remainder value when you divide rand() by 8. (rand() is the random number generation function).
I didn't supply any code, since this sounds like a school assignment.
Regards,
Paul McKenzie
You have to seed the generation with the current time using srand() when doing 1 & 2. Yes, even with random_shuffle(). Else random_shuffle() will keep giving you the same set of shuffled numbers.
Since this looks like a school assignment, I left that for the OP to discover.Quote:
Originally posted by CBasicNet
You have to seed the generation with the current time using srand() when doing 1 & 2. Yes, even with random_shuffle(). Else random_shuffle() will keep giving you the same set of shuffled numbers.
Regards,
Paul McKenzie
Hey Paul, this is not a school assignment. I am learning c++ on my own.
I did research on rand(), but found it difficult to understand.
I successfully implemented random_shuffle() from looking up in the msdn online library.
I still can't figure out how to generate the random numbers (0-7) such that they can repeat.
rand() returns a number between 0 and RAND_MAX (usually ~32000). To get a random number between 1 and 8, you would typically take the 8th modulus ( % operator ) or the random number. The other option is to divide by RAND_MAX and multiply by 8. This will give you a number between 0 and 7.
int r = rand() % 8;
int r = 8 * rand() / RAND_MAX;
Thanks mwilliamson.
I was having trouble with the syntax.
btw. you meant:
Plus, shouldn't we set RAND_MAX as a multiple of 8 and 7 respectively? (so we can generate 0 and 7 respectively)Code:rand() returns a number between 0 and RAND_MAX (usually ~32000).
To get a random number between 0 and 7, you would typically take the 8th modulus ( % operator ) of the random number.
The other option is to divide by RAND_MAX and multiply by 7. This will give you a number between 0 and 7.
int r = rand() % 8;
int r = 7 * rand() / RAND_MAX;
doumalc++ : if you are interested in the algorithm of shuffling, here's a simple program demonstrating that.
Code:#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int arr[10];
//assign the numbers 1-10 to the elements of the array
for(int cnt=0;cnt<10;++cnt)
arr[cnt]=cnt+1;
srand( (unsigned)time( NULL ));
//The code below re-shuffles them 4 times
for(int times=0;times<4;++times)
for(int cnt2=0;cnt2<10;++cnt2)
{
int i = (rand()%10);
int tmp = arr[i];
arr[i] = arr[cnt2];
arr[cnt2] = tmp;
}
//The code below displays them
for (int cnt3=0;cnt3<10;++cnt3)
cout<<arr[cnt3]<<endl;
return 0;
}
The formula should beQuote:
Originally posted by doumalc++
Plus, shouldn't we set RAND_MAX as a multiple of 8 and 7 respectively? (so we can generate 0 and 7 respectively)
int r =8*rand()/(RAND_MAX+1);
There is no way you can get 8 here.
:)Quote:
Originally posted by CBasicNet
The formula should be
int r =8*rand()/(RAND_MAX+1);
There is no way you can get 8 here.
:)Quote:
Originally posted by CBasicNet
The formula should be
int r =8*rand()/(RAND_MAX+1);
There is no way you can get 8 here.