|
-
February 12th, 2003, 06:10 PM
#1
Please help with random number generation
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
-
February 12th, 2003, 06:33 PM
#2
Re: Please help with random number generation
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
-
February 12th, 2003, 07:38 PM
#3
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.
-
February 12th, 2003, 10:01 PM
#4
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
-
February 12th, 2003, 10:26 PM
#5
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.
-
February 12th, 2003, 11:00 PM
#6
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;
-
February 13th, 2003, 12:16 AM
#7
Thanks mwilliamson.
I was having trouble with the syntax.
btw. you meant:
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;
Plus, shouldn't we set RAND_MAX as a multiple of 8 and 7 respectively? (so we can generate 0 and 7 respectively)
Last edited by doumalc++; February 13th, 2003 at 12:35 AM.
-
February 13th, 2003, 01:49 AM
#8
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;
}
-
February 13th, 2003, 01:53 AM
#9
[Edited : brain fart here]
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.
Last edited by CBasicNet; February 13th, 2003 at 02:31 AM.
-
February 13th, 2003, 04:10 AM
#10
Re: [Edited : brain fart here]
Originally posted by CBasicNet
The formula should be
int r =8*rand()/(RAND_MAX+1);
There is no way you can get 8 here.
-
February 13th, 2003, 04:22 AM
#11
Re: [Edited : brain fart here]
Originally posted by CBasicNet
The formula should be
int r =8*rand()/(RAND_MAX+1);
There is no way you can get 8 here.
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
|