Random string generator generates same string

This is a random string generator. It works fine but there is a problem. When I try to generate two or more string, it always generates same string (same X and Y). What change I need to make if I want to generate different string?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
string generateString(int length)
{
    static const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int strLen = sizeof(alphabet)-1;
    char genRandom;
    string str;
    srand(time(0));
    for(int i=0; i<length; i++)
    {
        genRandom = alphabet[rand()%strLen];
        str += genRandom;
    }
    return str;
}

int main()
{
    string X, Y;
    X=generateString(10);
    Y=generateString(10);
    cout << X << endl << Y<< endl;
}
Last edited on
Move line 7 to the top of main()
Thanks a lot :)
Topic archived. No new replies allowed.