random number in sequence

Hi,

I am making a program that requires you to get 3 random numbers in rapid sequence. I created a function and i call that function 3 times. it is setup with srand(time(0)) however I'm guessing it is all done in less than a second because all 3 numbers are the same. any ideas on how to fix that problem?

Thanks

1
2
3
4
for (int i = 0; i<3; i++)
{
        number[i] = populate();
}


1
2
3
4
5
6
7
8
9
int SimulationUI::populate()
{
	srand(time(0));
	int number = rand() * 1000;
	number += rand();
	
	number = number%(5000000 - 1000000) + 1000001;
	return number;
}
You should call srand(time(0)); once and only once in a whole program. You are correct, because the random number generator is being seeded with the same time, you get the same random number.

Solution:
Move the srand(time(0)) call to the start of your main().

Edit:
Why does your populate function do all of those operations over and over to number? I am guessing that you are trying to achieve "maximum randomness" or something.

Just so that you know, the number is most random when it has come directly from that function. Any operations after this that you perform will actually make it conform to a distribution. This is known as the central limit theorem in statistics.

Lets say that we want to simulate a dice roll:
(rand()%6)+1 gives us a number from 1 to 6. If we do this: ((rand()%6)+1) + ((rand()%6)+1) then we will have a higher probability of getting 7 than 2 or 12.

Likewise doing this: number = rand()%1000 * rand()%1000 we will get a convergence where there is a larger probability of getting 250000 than anything else.
Last edited on
i need a number between 1000000 and 5000000 and the max i can get is 32767 so i get that number and multiply by 1000 and add another random number and add to that. this way my max is actually 32799767. then i mod the number with (5000000-1000000) and add the min value 1000000. is there a better way of doing this?

i changed the function to receive a number and add that to srand. this way i can get different numbers but they are all still very close together and always around high 2000000s and low 3000000s.

1
2
3
4
for (int i = 0; i<3; i++)
{
        number[i] = populate((i+7)^4);
}


1
2
3
4
5
6
7
8
9
int SimulationUI::populate(int n)
{
	srand(time(0)+n);
	int number = rand() * 1000;
	number += rand();
	
	number = number%(5000000 - 1000000) + 1000001;
	return number;
}
First: srand() should not be called more than once! Take it out of the populate function.

Now:
RAND_MAX defined the maximum number that is generated. It is guaranteed to be at least 32767 but varies depending on implementation.

Lets consider hexidecimal for a moment. If we get a random number between 0 and 15, but we need a random number between 50 and 60, then we'll need to use more than one digit. Now lets consider a number system with base RAND_MAX. The same rule applies. Therefore this would be the correct way to do this:

1
2
3
4
5
6
7
8
9
10
11
12
int SimulationUI::populate(int min, int max)
{
	int number = rand();
	for (int i = 1; (RAND_MAX+1) * i < (max-min); i++)
	{
		number *= RAND_MAX+1; // bit-shift
		number += rand();     // Populate the lower bits now
	}
	
	// number is now a random number between 0 and (RAND_MAX+1)^i.  Now filter out values that don't fall within your required range. 
	return number%(max-min) + min;
}


Why?
Let's consider an example where rand() only gives us an output between 0 and 15. In binary RAND_MAX would look like:
0000 1111
So we can see that only the first four bits will be set. After we multiply our random number by RAND_MAX+1 (16 in dec, 0x10 in hex, 0001 0000 in binary) it will be in this range:
1111 0000
So if we add another rand() to it, we'll get a number in this range:
1111 1111
There, we've effectively changed RAND_MAX from 15 to 255. We can make it as large as we like assuming that it will fit in a container.

Oh, and for reference, 32767 looks like this in binary:
0000 0000 0000 0000 0111 1111 1111 1111
Last edited on
Topic archived. No new replies allowed.