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 ?

Recommended Answers

All 3 Replies

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;
}

thanks man it realy hellped

thanks..the srand seeding concept works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.