You are invited to Log in or Register a free Frihost Account!

# [c++]random number generator doesnt work

masterevil
i have coded a random number generator.

 Code: int d(int a, int b=0) {    int c;    int d;    d = a - b;    randomize();    c = random(d);    c = c + b;    return c; };

Function is that.
When i use that code:

 Code: cout << d(20) << " o " << d(20);

it prints the sama number.
for example, 4 o 4 or 5 o 5. Why does d(20) returns the same number?
i used randomize(); but this still dont work.
mathiaus
http://www.frihost.com/forums/vt-22047.html
SystemWisdom
You should only call randomize() once, otherwise you will reset it, and that's why you get the same number.

Take the randomize() call out of your function, and call it once at the start of your code...

 Code: // [...] int d(int a, int b=0) {    int c;    int d;    d = a - b;    c = random(d);    c = c + b;    return c; } // [...] int main(void) {     randomize();     cout << d(20) << " o " << d(20);     return 0; } // [...]

Should work... I didn't analyze your pseudo-random number logic though..
masterevil
hmm using once affects all the program, right?

By the way, when i want to find a random number between 20 and 30, i use d(30, 20).