home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_08 / 2n08057b < prev    next >
Text File  |  1991-06-10  |  962b  |  64 lines

  1. /* 
  2.  * Some simple routines to map the minimal standard
  3.  * random number generator into ranges and distributions
  4.  * of your choice.
  5.  */
  6.  
  7. /*
  8.  *    urand(): returns uniformly distributed
  9.  *    integers in the range lo..hi
  10.  */
  11.  
  12. int urand(int lo, int hi)
  13. {
  14. long tmp;
  15.  
  16.     tmp = stdrand();
  17.     return((int)(tmp * (long)(hi - lo) + (long)lo));
  18. }
  19.  
  20. /*
  21.  *    nrand(): returns normally distributed
  22.  *    integers in the range lo..hi
  23.  */
  24.  
  25. int nrand(int lo, int hi)
  26. {
  27. long tmp;
  28. int i;
  29.  
  30.     for(tmp=0, i=0; i<4; i++) {
  31.         tmp += urand(lo,hi);
  32.     }
  33.     tmp = (tmp + 2L) / 4L;
  34.  
  35.     return((int) tmp);
  36. }
  37.  
  38. /*
  39.  *    furand():
  40.  *    returns uniform random numbers in the
  41.  *    range 0.0 to 1.0 (exclusive)
  42.  */
  43.  
  44. float furand()
  45. {
  46.     return(stdrand() / 2147483647.0);
  47. }
  48.  
  49. /*
  50.  *    returns normal random numbers in the
  51.  *    range 0.0 to 1.0 (exclusive)
  52.  */
  53.  
  54. float fnrand()
  55. {
  56. float tmp;
  57. int i;
  58.  
  59.     for(tmp=0.0, i=0; i<4; i++) {
  60.         tmp += furand();
  61.     }
  62.     return(tmp / 4.0);
  63. }
  64.