I'm making a sort of text based version of http://www.educationalsimulations.com/products.html . Looking it up on youtube will give you a better idea. So anyway, I'm using srand/rand to first pick a gender, than pick a name, from a random number, according to gender, this is what I got:
int main()
{
srand ( time(NULL) );
int gen;
gen = rand() % 2 + 1;
int nam;
string name;
if (gen == 1)
{
nam = rand() % 4 + 1;
if (nam == 1);
{
name = "Bill S. Preston Esq.";
}
if (nam == 2);
{
name = "Ted Teodore Logan";
}
if (nam == 3);
{
name = "Joe";
}
if (nam == 4);
{
name = "Bob";
}
}
if (gen == 2)
{
nam = rand() % 4 + 1;
if (nam == 1);
{
name = "Sally";
}
if (nam == 2);
{
name = "Marla";
}
if (nam == 3);
{
name = "Mary";
}
if (nam == 4);
{
name = "Jane";
}
}
cout << gen << name << endl;
return 0;
}
And when I compile, the gender picking is fine, but the name it'll pick for each gender will always be Bob/Jane, accordingly. Any help or ideas of whats wrong?
#include <iostream>
#include <string>
#include <time.h>
usingnamespace std;
int main ()
{
srand ( (unsigned) time (NULL) );
int Nam , Gen;
string Name = "";
Gen = rand () % 2 + 1;
if (Gen == 1)
{
Nam = rand () % 4 + 1;
if (Nam == 1)
{
Name = "Bill S. Preston Esq.";
}
if (Nam == 2)
{
Name = "Ted Teodore Logan";
}
if (Nam == 3)
{
Name = "Joe";
}
if (Nam == 4)
{
Name = "Bob";
}
}
elseif (Gen == 2)
{
Nam = rand () % 4 + 1;
if (Nam == 1)
{
Name = "Sally";
}
if (Nam == 2)
{
Name = "Marla";
}
if (Nam == 3)
{
Name = "Mary";
}
if (Nam == 4)
{
Name = "Jane";
}
}
cout << Gen << " " << Name << endl;
cout << "Press ENTER to end the program" << endl;
cin.get ();
return 0;
}
Oh if I seed to much I might actually break some of the statistical properties of the random numbers.
So I should seed once in the beginning of an application.
#include <iostream>
#include <string>
#include <time.h>
usingnamespace std;
int main ()
{
srand ( (unsigned) time (NULL) );
int Nam , Gen;
string Name = "";
Gen = rand () % 2 + 1;
if (Gen == 1)
{
Nam = rand () % 4 + 1;
if (Nam == 1)
{
Name = "Bill S. Preston Esq.";
}
if (Nam == 2)
{
Name = "Ted Teodore Logan";
}
if (Nam == 3)
{
Name = "Joe";
}
if (Nam == 4)
{
Name = "Bob";
}
}
elseif (Gen == 2)
{
Nam = rand () % 4 + 1;
if (Nam == 1)
{
Name = "Sally";
}
if (Nam == 2)
{
Name = "Marla";
}
if (Nam == 3)
{
Name = "Mary";
}
if (Nam == 4)
{
Name = "Jane";
}
}
cout << Gen << " " << Name << endl;
cout << "Press ENTER to end the program" << endl;
cin.get ();
return 0;
}
The seed is used to generate a pseudo-random sequence of numbers, given the same seed, you'll generate the same numbers. As is happening in the example I posted, the returned value of time(0) has yet to change.