home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FRAND.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  1KB  |  41 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  FRAND.C - Public domain by Larry Hudson
  5. */
  6.  
  7. #include <math.h>
  8. #include <time.h>
  9. #include "snipmath.h"
  10.  
  11. #define TEN_PI 31.41592653589793
  12. #define E      2.718281828459045
  13.  
  14. /*--------------------------------------------------------------+
  15. |           Return random double between 0.0 and 1.0            |
  16. |                                                               |
  17. |    If n is negative it will randomize the seed, based on the  |
  18. |        current MSDOS time.                                    |
  19. |    If n is zero it will return the next random number.        |
  20. |    If n is positive it will set the seed to a value based on  |
  21. |        the value of n.                                        |
  22. +--------------------------------------------------------------*/
  23.  
  24. double frandom(int n)
  25. {
  26.       static double seed = E;
  27.       double dummy;
  28.       time_t tim;
  29.  
  30.       if (n < 0)
  31.       {
  32.             time(&tim);
  33.             seed = (double)tim;
  34.       }
  35.       else if (n > 0)
  36.             seed = (double)n * E;
  37.  
  38.       seed = modf(seed * TEN_PI + E, &dummy);
  39.       return seed;
  40. }
  41.