home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c005 / 5.ddi / C / UTRND.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-08-05  |  1.2 KB  |  44 lines

  1. /**
  2. *
  3. * Name        utrnd -- Random number generator
  4. *
  5. * Synopsis    rnd = utrnd(pseed);
  6. *
  7. *        float     rnd      The returned random value, in the
  8. *                  range from 0 to 1.
  9. *        unsigned *pseed   Pointer to a seed value.  It is updated
  10. *                  on each successive call to UTRND.
  11. *
  12. * Description    UTRND is a linear congruential pseudorandom number
  13. *        generator.  If the seed value is even, it uses the
  14. *        system clock to find a seed; otherwise it uses the
  15. *        passed value and then updates the seed on successive
  16. *        calls.    The value returned is the updated seed divided
  17. *        by 65536.0.
  18. *
  19. * Returns    rnd          Random number between 0 and 1.
  20. *        pseed          Updated seed value
  21. *
  22. * Version    3.0 (C)Copyright Blaise Computing Inc.    1983, 1984, 1986
  23. *
  24. **/
  25.  
  26. #include <bquery.h>
  27.  
  28. float utrnd(pseed)
  29. register unsigned *pseed;
  30. {
  31.     int hours,minutes,seconds,hunds;
  32.  
  33.     if ((*pseed % 2) == 0)
  34.     {
  35.        qyrettim(&hours,&minutes,&seconds,&hunds);
  36.        *pseed = (unsigned) (seconds * hunds);
  37.        if ((*pseed % 2) == 0)          /* Make sure the seed is odd    */
  38.       (*pseed)++;              /* to ensure maximum period     */
  39.     }
  40.     *pseed *= 15625;              /* Multiplication modulo 65536  */
  41.  
  42.     return((float)(*pseed/65536.0));
  43. }
  44.