S
Sonoman
Hi all:
I am trying to write a simple program that simulates asking several persons
their birth day and it counts how many persons are asked until two have the
same birth day. The problem that I have is that the first loop I get a
sequence of random numbers untuil I get a match, BUT then on the following
loops I get the SAME random(?) sequence. I am using rand(). I do not want to
get too fancy with the random number generator, but is there a way of
stopping rand() from resetting every time it starts the loop? What other
choices do I have? Can I somehow use the system time to help me get more
random? Here is my code so far:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int const tests = 10000;
int index;
int i,j;
int match = 0;
int days[366] = {0};
int count = 0;
for (j = 1; j <= tests; j++)
{
while (match != 1)
{
index = (rand() % 365) + 1;
if (days[index] != 1)
{
days[index]++;
count++;
}
else
{
for (i = 1; i < 366; i++)
{
days = 0;
}
match = 1;
}
}
cout << count << endl;
}
return 0;
}
I am trying to write a simple program that simulates asking several persons
their birth day and it counts how many persons are asked until two have the
same birth day. The problem that I have is that the first loop I get a
sequence of random numbers untuil I get a match, BUT then on the following
loops I get the SAME random(?) sequence. I am using rand(). I do not want to
get too fancy with the random number generator, but is there a way of
stopping rand() from resetting every time it starts the loop? What other
choices do I have? Can I somehow use the system time to help me get more
random? Here is my code so far:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int const tests = 10000;
int index;
int i,j;
int match = 0;
int days[366] = {0};
int count = 0;
for (j = 1; j <= tests; j++)
{
while (match != 1)
{
index = (rand() % 365) + 1;
if (days[index] != 1)
{
days[index]++;
count++;
}
else
{
for (i = 1; i < 366; i++)
{
days = 0;
}
match = 1;
}
}
cout << count << endl;
}
return 0;
}