0

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!

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by slimjimmer
0
#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;
}
This question has already been answered. 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.