4

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?

Community
  • 1
  • 1
akway
  • 1,738
  • 4
  • 21
  • 20
  • 2
    exact duplicate: http://stackoverflow.com/questions/1108780/why-do-i-always-get-the-same-sequence-of-random-numbers-with-rand – Kip Jul 27 '09 at 21:33
  • 1
    Check out this question for your answer about it only changing with time. http://stackoverflow.com/questions/1068350/random-number-function-is-misfiring You need to only seed once, at the beginning of your program. – GManNickG Jul 27 '09 at 21:33

6 Answers6

8

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
  • 11,763
  • 15
  • 70
  • 103
  • 2
    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 Jul 27 '09 at 21:27
  • 2
    @Graeme - The word you're looking for in that situation is "Catch-22." – Chris Lutz Jul 27 '09 at 22:13
8

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
  • 114,894
  • 38
  • 205
  • 284
4

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
  • 118,630
  • 17
  • 138
  • 146
4

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.

John Carter
  • 53,924
  • 26
  • 111
  • 144
  • 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 C Jul 27 '09 at 21:33
  • @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 Jul 28 '09 at 01:38
2

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
  • 2,804
  • 6
  • 32
  • 41
1

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

nlucaroni
  • 47,556
  • 6
  • 64
  • 86