Advice with this Random Person function

Mar 9, 2015 at 4:45pm
So this code works as is. You can run this code by itself and have it cout the details and you'll see that its always a different person. My problem is that when it is in a loop it always returns the same person. The class is there too if that helps. I also have a Random String function in this same program that is in a loop, and that one doesn't return the same string. Should I post that one too? Any advice?

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  class person
{
public:
    //private:
    int Age;
    string Fname;
    string Lname;
    string SSN;
    char Gen;
    
    void fillAge(int a)
    {
        Age = a;
    }
    void fillName(string f, string l)
    {
        Fname = f;
        Lname = l;
    }
    void fillSocial(string s)
    {
        SSN = s;
    }
    void fillGen(char g)
    {
        Gen = g;
    }
    
    //public:
    
    void createPerson(int age, string fname, string lname, string ssn, char gen)
    {
        fillAge(age);
        fillName(fname, lname);
        fillSocial(ssn);
        fillGen(gen);
    }
    
};

// Random person
person createRndPrsn()
{
    person p;
    int persposf=0, persposl=0, numpos=0,a=0;
    string RFN, RLN, ssn;
    char g;
    string fnames[]={"Amy","John","Mary","Thomas","Stacey","Robert","Selina","Mathew","Nancy","Nicholas",
        "Danielle","Michael","Laura","Max","Irma","Peter","Melinda","Gunnar","Arya","Connor",
        "Layla","Ezio"};
    string lnames[]={"Smith", "Williams","Garcia","King","Reynolds","Brooks","Gray","Stevens","Washington",
        "Olson","Freeman","Moreno","Cole","Auditore","Knight","Harrison","Morrison","May","Chen",
        "Bates","Norris","Graves"};
    string snums="0123456789";
    
    
    // get random num
    srand(time(0)%100);
    persposf=rand()%22;
    
    srand(time(0));
    persposl=rand()%22;
    
    // Set age
    srand(time(0)%7);
    a=rand()%100+1;
    
    // Set gender based on first name
    if (persposf%2 == 0)
    {
        g='F';
    }else
    {
        g='M';
    }
    
    // create ssn based on rnd num generator
    for (int i=0; i<8;i++)
    {
        if(i == 0)
        {
            srand(time(NULL));
            numpos=rand()%9+1;
            ssn=snums[numpos];
        }
        srand(i);
        numpos=rand()%9;
        ssn+=snums[numpos];
    }
    
    // use rnd nums to get name
    RFN=fnames[persposf];
    RLN=lnames[persposl];
    
    // fill in person detials
    p.createPerson(a, RFN, RLN, ssn, g);
    
    return p;
};
Mar 9, 2015 at 4:50pm
You should only call srand() once at the start of your program, and then never again.

By the way, rand() and srand() are being deprecated - don't use them anymore. Instead, use the new random generators available in the <random> header:
http://www.cplusplus.com/reference/random/
http://en.cppreference.com/w/cpp/header/random
Topic archived. No new replies allowed.