home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0020 - 0029 / ibm0020-0029 / ibm0028.tar / ibm0028 / GRLF-C-2.ZIP / GFUNC / RANDFNS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-30  |  1.1 KB  |  66 lines

  1. /*
  2.  * rand.c
  3.  * contains: gfsrand(),gfrand(),nfrom()
  4.  *
  5.  *   Copyright (C)1987-1990 Greenleaf Software Inc. All Rights Reserved.
  6.  *
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include "gfuncts.h"
  11.  
  12. static long rnum;
  13.  
  14. /*
  15.  *  void
  16.  * gfsrand(x)
  17.  *
  18.  * ARGUMENT
  19.  *  (short)    x    -    value to initialize rnum with.
  20.  *
  21.  * DESCRIPTION
  22.  *  This function sets variable rnum with the parameter to gfsrand()
  23.  */
  24. void GF_CONV gfsrand(num)
  25. short num;
  26. {
  27.     rnum=(long)num;
  28. }
  29.  
  30. /*
  31.  *  short
  32.  * gfrand(void)
  33.  *
  34.  * DESCRIPTION
  35.  *  Generates pseudorandom number of period 2^32 on the interval 0..32768
  36.  *
  37.  * RETURNS
  38.  *  pseudorandom integer between 0 an 32768
  39.  */
  40. short GF_CONV gfrand()
  41. {
  42.     rnum=rnum*0x41C64E6DL+0x00003039L;
  43.     return((short)(rnum>>16)&0x7FFF);
  44. }
  45.  
  46. /*
  47.  *  short
  48.  * nfrom(lo,hi)
  49.  *
  50.  * ARGUMENT
  51.  *  (short)    lo    -    lower limit
  52.  *  (short)    hi    -    upper limit
  53.  *
  54.  * DESCRIPTION
  55.  *  generates pseudorandom integer between the limits specified.
  56.  *
  57.  * RETURNS
  58.  *  pseudorandom integer between the limits specified.
  59.  */
  60. short GF_CONV nfrom(lo,hi)
  61. register short lo,hi;
  62. {
  63.     register short nb=hi-lo+1;
  64.     return(gfrand()%nb+lo);
  65. }
  66.