doumalc++
February 12th, 2003, 05:10 PM
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
1. none of the 8 number repeats
2. the 8 numbers can repeat
|
Click to See Complete Forum and Search --> : Please help with random number generation doumalc++ February 12th, 2003, 05:10 PM 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 Paul McKenzie February 12th, 2003, 05:33 PM 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 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. 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 CBasicNet February 12th, 2003, 06:38 PM 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. Paul McKenzie February 12th, 2003, 09:01 PM 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. Since this looks like a school assignment, I left that for the OP to discover. Regards, Paul McKenzie doumalc++ February 12th, 2003, 09:26 PM 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. mwilliamson February 12th, 2003, 10:00 PM 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; doumalc++ February 12th, 2003, 11:16 PM Thanks mwilliamson. I was having trouble with the syntax. btw. you meant: 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; Plus, shouldn't we set RAND_MAX as a multiple of 8 and 7 respectively? (so we can generate 0 and 7 respectively) CBasicNet February 13th, 2003, 12:49 AM doumalc++ : if you are interested in the algorithm of shuffling, here's a simple program demonstrating that. #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; } CBasicNet February 13th, 2003, 12:53 AM 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) The formula should be int r =8*rand()/(RAND_MAX+1); There is no way you can get 8 here. doumalc++ February 13th, 2003, 03:10 AM Originally posted by CBasicNet The formula should be int r =8*rand()/(RAND_MAX+1); There is no way you can get 8 here. :) doumalc++ February 13th, 2003, 03:22 AM Originally posted by CBasicNet The formula should be int r =8*rand()/(RAND_MAX+1); There is no way you can get 8 here. :) codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |