# problem with random numbers

• 11-24-2005
xxwerdxx
problem with random numbers
hi i just made a program to practice random numbers because im new :). i get different numbers each time, but every time i run the program they are the same numbers each time. anyone know the problem? thanks in advance.

Code:

```#include<stdio.h> #include<stdlib.h> int number(void); main() {             number();       number();       number();             printf("hit a key to quit");       getchar();             return 0;       }       int  number(){     int fy=1,fz=100,fx;     fx=rand() % (fz-fy+1);     printf("Your number is %d\n",fx);     return fx;     }```
• 11-24-2005
fischerandom
What you are talking about is not random numbers, they are pseudo-random numbers. A pseudo-random generator such as the standard C library function rand( ) uses a start seed and from that start seed there will be a mathematically defined sequence of pseudo-random numbers returned each time it is called. To get a new sequence each time you run your program you need to set a different seed with srand( ). This can be accomplished by using the current time like this:
Code:

```#include <stdlib.h> #include <stdio.h> #include <time.h> int  main( ) {  int  n;    srand( time( 0 ) );  /* Generate a new seed */    for ( n = 0; n < 100; ++n )   printf( "%i, ", rand( ) );    return 0; }```
• 11-24-2005
dwks
See Prelude's site.

http://eternallyconfuzzled.com/articles/rand.html
[/edit]