views:

538

answers:

6

Possible Duplicate:
why do i always get the same sequence of random numbers with rand() ?

This is my file so far:

#include <stdio.h>

int main(void) {
    int y;
    y = generateRandomNumber();
    printf("\nThe number is: %d\n", y);
    return 0;
}

int generateRandomNumber(void) {
    int x;
    x = rand();
    return x;
}

My problem is rand() ALWAYS returns 41. I am using gcc on win... not sure what to do here.

EDIT: Using time to generate a random number won't work. It provides me a number (12000ish) and every time I call it is just a little higher (about +3 per second). This isn't the randomness I need. What do I do?

+8  A: 

you need to provide it a seed.

From the internet -

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
  int i, stime;
  long ltime;

  /* get the current calendar time */
  ltime = time(NULL);
  stime = (unsigned) ltime/2;
  srand(stime);

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

  return 0;
}
Steve
Note that time(NULL) is usually good enough, but there are cases where you want more entropy so you need to do more work to come up with a good enough seed. It always seemed weird to me that in order to generate good pseudo-random numbers (the output from rand()), you first need to come up with a good pseudo-random number (the seed).
Graeme Perrow
@Graeme - The word you're looking for in that situation is "Catch-22."
Chris Lutz
+3  A: 

Try calling srand(time(NULL)); before your call to generateRandomNumber.

As others have stated, you need to call srand() one time when the application starts up, not every time that you call rand().

Sean Bright
This will not work because it gives me a slightly increased number if I call it again.
akway
Make sure you seed ONCE, not on each call to rand().
Fred Larson
You are only supposed to seed the random number generator once
1800 INFORMATION
My previous comment was @akway. My point was that doing the srand() call before each rand() call will result in poor random numbers.
Fred Larson
mine too. Sounds like we are all in agreement
1800 INFORMATION
+6  A: 

Standard trick is:

srand(time(0));  // Initialize random number generator.

NOTE: that function is srand, not rand.

Do this once in your main function. After that, only call rand to get numbers.

Depending on the implementation, it can also help to get and discard a few results from rand, to allow the sequence to diverge from the seed value.

Daniel Earwicker
+1  A: 

as before, a seed. Often times something like, time() or pid()

nlucaroni
+2  A: 

Sometimes compilers use the same random seed to help debugging easier which is great but defeats the purpose of randomize. There are code examples on how to seed your random generator with some dataset, usually system time but this is the explanation behind why you got the same number over and over.

Javed Ahamed
+3  A: 

This is because rand() is implicitly seeded to 1.

If you don't want the same sequence of numbers each time you run your program, you should set the seed (using srand()) when the program starts, with a value that changes from one run to another.

The clock time is a popular choice for this, but bear in mind that this is predictable, and could therefore be a vulnerability if you want your number sequence to be unpredictable.

therefromhere
Huh. I would have guessed that the initial seed would have been left unspecified (like seemingly everything else in C), but sure enough, it's right there in the manpage: "If no seed value is provided, the rand() function is automatically seeded with a value of 1."
Jason Creighton
@Jason, having the sequence be determined by a fixed seed aids testing the library. Real applications need to seed the generator anyway, and often need a better generator than `rand()` as well.
RBerteig