I'm having a bit of trouble with this random number generation function. It's going to be part of a much larger program which requires lots of random numbers so I decided to use time as a seed. However I can't seem to get this to work. This code is virtually taken from my lecturers notes and I have tried a lot of variations but can't figure what's wrong.

Here's the code:

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

int main()
{
int random, seed, time;

seed = (int) time(NULL);
random = rand(seed);

printf("random number = %d\n", random);

return (0);
}

Hope someone can help!

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

int main ( void )
{
  int r;

  srand ( (unsigned)time ( NULL ) );
  r = rand();

  printf ( "random number = %d\n", r );

  return 0;
}

Thanks for that

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.