Bruce
Bruce

Reputation: 35285

Generating random numbers in C

I know this question has been asked time and again. I need random numbers between 0-9. I am using the following code:

srand(time());
int r;
for (;;)
{
    while(condition)
    {
        r = rand()%10;
        // ...
        // ...
    }
}

Now I am getting the same sequence of numbers for each iteration of for loop. Can someone provide me the correct code?

PS - My compiler doesn't recognize arcrandom().

Upvotes: 0

Views: 1202

Answers (9)

Mark Ransom
Mark Ransom

Reputation: 308520

The only possible way you could be getting the exact same sequence of numbers is if there is a call to srand() somewhere in the code that you haven't shown us. It could be deep within a function call somewhere.

The proper way to fix this would be to find the extra call to srand and remove it. However this quick hack might get you started:

static int seed = time();
int r;
for (;;)
{
    srand(seed++);
    while(condition)
    {
        r = rand()%10;
        // ...
        // ...
    }
}

Upvotes: 0

Andrew Edgecombe
Andrew Edgecombe

Reputation: 40382

Depending on your requirements for statistical randomness you might find it better to ditch the psuedo random number generator and try an external source.

random.org and randomnumbers.info (amongst others) will provide truly random numbers for download, which libcurl should handle for you.

Upvotes: 0

EvilTeach
EvilTeach

Reputation: 28882

Many pseudorandom generators have an observable period.
If you need a long period consider using the algorithm from Knuth 2. It has a period of about 2^55, and is simple to implement.
RANARRAY

Upvotes: 2

pmg
pmg

Reputation: 108988

Remove all srand() calls from your code.

Add one, unique call right before the first statement in main().

int main(void) {
    int foo = 42;
    int bar = baz(42);

    srand(time(0));
    /* rest of your program */
    return 0;
}

Upvotes: 1

Malx
Malx

Reputation: 988

Have you random()?

random() man page

I have made simple program:

int i;
for(i=0;i<40;i++)
printf("%d",rand()%10);

And geting 7938024839052273790239970398657627039991 - no repeating here. How long is your sequence?

Upvotes: 0

Paul Nathan
Paul Nathan

Reputation: 40319

srand(time(0));

r = rand() % (max+1);

//r will be a random number between 0 and max.

I can only think that you did not initialize correctly, or your redacted code is redacted wrongly.

Upvotes: 0

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234644

This problem stems from the fact that on some implementations the lower-order bits of rand() are not very random. Quoting from the man page:

"If you want to generate a random integer between 1 and 10, you should always do it by using high-order bits, as in

j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0)));

and never by anything resembling

j = 1 + (rand() % 10);

(which uses lower-order bits)."

Upvotes: 13

ephemient
ephemient

Reputation: 204964

Your code doesn't compile -- for() is not valid C.

Are you sure that you are calling srand only once, at the start of the program and before any rand calls? Calling srand within the loop would explain this behavior.

Upvotes: 0

Corey
Corey

Reputation: 3205

You're probably calling this function within the second, and srand is going to get seeded with the same second. You should only call srand once at the beginning of main.

Upvotes: 7

Related Questions