How do you get 20 random integers between 1 and 20, and all the numbers are different from each other all the time guaranteed?
Five answers:
husoski
2010-06-29 12:20:27 UTC
This is commonly called a "shuffle". The easiest way I know to do this and get an unbiased distribution (every number equally-likely in every position) is to put the numbers in an array. To shuffle, loop through list/array and exchange element a[i] with a randomly-selected element a[k], chosen such that i <= k < n, where n is the array size. (When k==i, you don't actually have to exchange anything.)
#include
#include
#include
using namespace std;
int main()
{
... int n = 20;
... int numbers[20];
... srand(time(NULL)); // reset the random number generator
... // First, initialize the list:
... for (int i=0; i ... ... numbers[i] = i+1;
... // Shuffle
... for (int i=0; i ... {
... ... int nleft = n - i;
... ... int k = i + (int)rand() % nleft;
... ... if (k != i)
... ... {
... ... ... int temp = numbers[i];
... ... ... numbers[i] = numbers[k];
... ... ... numbers[k] = temp;
... ... }
... }
... // Display:
... cout << "Shuffled:";
... for (int i=0; i ... cout << endl;
}
Note: The expression "rand() % nleft" generates a random number in the range 0..(nleft-1). This is NOT the best way to generate small random numbers, but it works well enough for casual applications.
2016-04-13 00:01:27 UTC
Here are two C++ functions I use for the purposes of getting random integers: int z(int min,int max) { return (rand() % ((max - min) + 1)) + min; } void s() { srand((unsigned)time(0)); } When the program starts, call s() to reseed the generator (otherwise you'll get the same sequence every time). Use z() with the (inclusive) integer limits as arguments to get a random integer. I have used these functions a great deal, and although they aren't perfectly random, for most purposes they work very well. Calling z(0,1) should get you a random integer, either 0 or 1, with very close to equal probabilities of each. However, for some purposes, the above functions do not suffice. Here are updated versions: int z(int min,int max) { return (((rand() * rand()) + rand()) % (max(1,max - min) + 1)) + min; } void s() { srand((unsigned)(time(0) + z(0,16384))); } Although they take a little longer to run, they have stronger capabilities that you might need for some types of applications. I have used these too, a lot, and they also seem to work very well.
Chris C
2010-06-29 11:30:55 UTC
1) Have an array of 20 integers defined (i.e. int goodRandom[20];).
2) Seed the random number generator based on the current time (i.e. srand(time(0));)
3) Define an int counter and initialize it to zero (i.e. int goodCount = 0;)
4) In a loop that ends when the counter is at 20 (i.e. while (goodCount < 20) ), do the next 4 lines:
4a) Call the random function (i.e. int tmp = rand(20);)
4b) Loop through the list of good integers (i.e. for (i = 0; i < goodCount; i++) )
4c) See if the random value is in the list and if it is exit early (i.e. if (tmp == goodRandom[i]) break; ).
4d) See if the counter to look through the list of good integers is 20 or less and if it is NOT, store the value into the list of good random integers and increment the number of good integers (i.e. goodRandom[goodCount++] = tmp;)
The Phlebob
2010-06-29 11:24:23 UTC
I would use the rand() function to build a list, biasing each return value so the result fits within the range, then skip any new values that duplicate one already in the list.
Hope that helps.
?
2010-06-29 11:27:26 UTC
I would use the C++ function random_shuffle() to rearrange 20 sequential numbers in random order: