srand/rand problems

closed account (967L1hU5)
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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?
Last edited on
Your if conditions are a bit wrong. There should be no semi-colon between if(...) and what you want it to do.

e.g.

1
2
3
4
if (nam == 1)
{
    name = "Sally";
}


not

1
2
3
4
if (nam == 1);
{
    name = "Sally";
}


Andy

P.S. i.e. the ; form is the same as

1
2
3
4
5
if(nam = 1)
{
    // do nothing
}
name = "Sally";


P.P.S. The "Source Code" tag -- the <> button -- will preserve your code formatting (and you can add it in retrospect!)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <time.h>
using namespace 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";
		}

	}
	else 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;
        cout << "Press ENTER to end the program" << endl;
	cin.get ();
	return 0;
}
Last edited on
closed account (DSLq5Di1)
@TheMassiveChipmunk
Bad advice.. you should only seed rand once during the life time of your application!
http://ideone.com/BlHle
@sloppy9
Sorry.
I never knew that do you know why?
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <time.h>
using namespace 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";
		}

	}
	else 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;
        cout << "Press ENTER to end the program" << endl;
	cin.get ();
	return 0;
}
closed account (DSLq5Di1)
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.

Yup just once in the beginning is fine. ^^
Last edited on
Topic archived. No new replies allowed.