random number, how do I do it?

Kenneth L Moore kenmoore at unix.cis.pitt.edu
Tue Nov 28 21:00:58 AEST 1989


In article <5726 at ozdaltx.UUCP> root at ozdaltx.UUCP (root) writes:
>I am trying to write some code to generate a random number that would
>fall between 0 and 65,000.  

>------------------
>Thanks
>Scotty
>AIDS INFORMATION EXCHANGE BBS      (214) 247-2367/247-5609
>               "Education is the best weapon"
>     {ames,rutgers,texsun,smu}!attctc!ozdaltx!sysop 


Here is a program that will generate the numbers you need.

To change the range of responses just change the 65000 in 
the subroutine.

The ran48 is seeded with the time and a static multiplier is
used for when the calls are so close that system time has not
changed.

X-----X-----X-----X-----X-----X cut here X-----X-----X-----X-----X-----X-----X

#include <time.h>
main()
{

long timeofday;
int ind,test,num1;

timeofday = time(0);
printf("\n The time is: \n %s\n",ctime(&timeofday));
ind = 0;
while(ind++ != 15)
  {
  num1 = sixtyfive();
  printf(" The number is %d \n",num1);
  }
}

int sixtyfive()
{
  static int xxx;
  long seedval;
  double drand48(),x;
  int num;
  seedval = time(0)+(long)xxx;
  srand48(seedval);
  xxx = seedval*drand48();
  x = drand48();
  num = (int)(65000*x);
return(num);
}

Sign me:

KK    KK  EEEEEEEE  NN     NN  MM       MM   OOOOO    OOOOO   RRRRRR   EEEEEEEE
KK   KK   EEEEEEEE  NNN    NN  MMM     MMM  OOOOOOO  OOOOOOO  RR   RR  EEEEEEEE
KK  KK    EE        NNNN   NN  MMMM   MMMM  OO   OO  OO   OO  RR  RR   EE
KKKKK     EEEEE     NN NN  NN  MM MM MM MM  OO   OO  OO   OO  RRRRR    EEEEE
KKKKK     EEEEE     NN  NN NN  MM  MMM  MM  OO   OO  OO   OO  RR RR    EEEEE
KK  KK    EE        NN   NNNN  MM   M   MM  OO   OO  OO   OO  RR  RR   EE
KK   KK   EEEEEEEE  NN    NNN  MM       MM  OOOOOOO  OOOOOOO  RR   RR  EEEEEEEE
KK    KK  EEEEEEEE  NN     NN  MM       MM   OOOOO    OOOOO   RR    RR EEEEEEEE

kenmoore at unix.cis.pitt.edu (Kenneth L. Moore)

(Sorry about the long sig but inews won't recognise my .signature...
even with "chmod 777 .signature". Tsk. Tsk.)



More information about the Comp.lang.c mailing list