Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Function..Arghness

Status
Not open for further replies.

ronnyjljr

IS-IT--Management
Nov 23, 2003
249
US
Here Goes...

I believe I am using the Srand and Rand functions correctly but I am calling them in a loop. I believe the loop is iterating too quickly and the rand() is not changing it's values.

Code:
srand( static_cast<unsigned int>(time( NULL )) );
int Card = (1+rand())%13;

If I do this, all card values are returned as 0. If I do not have the srand the function returns random numbers but they are the same everytime. What can I do to over come this?

Secondly, I know that the number is not changing because I am adding them up in an array, and one specific element will receive the numbers of times the for loop has run. Where the rand() function always returned 3. Any help?

Code:
OutPut:
[green]

        myFreq[ 0]                 0
        myFreq[ 1]                 0
        myFreq[ 2]                 0
        myFreq[ 3]              1000
        myFreq[ 4]                 0
        myFreq[ 5]                 0
        myFreq[ 6]                 0
        myFreq[ 7]                 0
        myFreq[ 8]                 0
        myFreq[ 9]                 0
        myFreq[10]                 0
        myFreq[11]                 0
        myFreq[12]                 0
        myFreq[13]                 0
        myFreq[14]                 0
        myFreq[15]                 0
        myFreq[16]                 0
        myFreq[17]                 0
        myFreq[18]                 0
        myFreq[19]                 0
        myFreq[20]                 0
        myFreq[21]                 0
        myFreq[22]                 0
        myFreq[23]                 0
        myFreq[24]                 0

[/green]

Thanks,
Ron

cout << "If you don't know where you want to go, we'll make sure you get taken";
 
> If I do this, all card values are returned as 0.
Check your cast

Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout;
using std::endl;

int main ( ) {
    unsigned int seed = static_cast<unsigned int>(time( NULL ));
    cout << "Seed is " << seed << endl;
    srand( seed );
    int Card = (1+rand())%13;
    cout << Card << endl;
}
Which seems to work just fine here...

My guess is that seed will be zero (for you, for some reason), and initialising a lot of pseudo random number generators with zero is a really bad idea.

--
 
The Seed is the same everytime.
Is this because I am running in a for loop and the random function is based on clock pulses, and therefor the clock has not changed and cannot generate another number?


Help!
Ron

cout << "If you don't know where you want to go, we'll make sure you get taken";
 
> The Seed is the same everytime.
Yes, you're only supposed to call srand() ONCE per run of the program, unless it's your intent to restart the sequence at the same point for some reason.

--
 
You hit the nail on the head with that one!
I completely forgot about srand properties, haven't used them since early summer.

Thanks Salem!

cout << "If you don't know where you want to go, we'll make sure you get taken";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top