hi everyone

i would like to fill an array with random numbers

i know i can use the next line

srand( time(NULL) );
return 1 + rand() % 10 ;

but the problem is that it fills

my array with only one number

what can i do ?

9 Years
Discussion Span
Last Post by mayur.sarode

srand seeds the random number generator. If you call srand too often, and the value you use as the seed (srand's argument) changes infrequently, you'll keep seeding the random number generator to the same thing and rand will produce the same sequence. This will give you an idea of how rand and srand work together, but the simple answer is that you should call srand once:

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

int gimme_random ( void )
  return 1 + rand() % 10;

int main ( void )
  int i;

  /* Call srand only once */
  srand ( (unsigned)time ( NULL ) );

  for ( i = 0; i < 10; i++ )
    printf ( "%d\n", gimme_random() );

  return 0;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.