gcc newbie: rand()

Rick Dearman rick@ricken.demon.co.uk
Sun Dec 12 15:35:00 GMT 1999


Try this:

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

int
get_rand(int range)
{
  int mrand;
  mrand = (int)((double)rand() / ((double)RAND_MAX +1) * range);
  return mrand;
}

int
main(void)
{
  float x;
  int n;

  srand((unsigned int)time((time_t *)NULL));

  x = (float)( get_rand(5) / 32768.0 );
  n = 1 + (int) (6*x);
	
  printf("x = %d\n", n);

  return 0;

}


More information about the Gcc-help mailing list