FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
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
please use tags for your topic titles
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).
Related topics
Microsoft confirms that XP contains random number generator
Visual Basic Tutorial - Random numbers
Astrology Love
C/C++
JAVA - random name generator
What lanugage should I learn next?
Psychic Abilities?
Javascript random code generator script needed please
Simple(?) probability question
lottery ^^
Frihost PowerCube Lotto *Mod Approved & Acclaimed*
My account doesnt work and mails don't come...
Frihost PowerCube Lotto Returns
Is lottery totally random?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.