home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / nfsrc21.zip / RAND1.PRG < prev    next >
Text File  |  1991-08-16  |  2KB  |  83 lines

  1. /*
  2.  * File......: RAND1.PRG
  3.  * Author....: Gary Baren
  4.  * CIS ID....: 75470,1027
  5.  * Date......: $Date:   15 Aug 1991 23:04:30  $
  6.  * Revision..: $Revision:   1.2  $
  7.  * Log file..: $Logfile:   E:/nanfor/src/rand1.prv  $
  8.  *
  9.  * This is an original work by Gary Baren and is hereby placed in the
  10.  * public domain.
  11.  *
  12.  * Modification history:
  13.  * ---------------------
  14.  *
  15.  * $Log:   E:/nanfor/src/rand1.prv  $
  16.  * 
  17.  *    Rev 1.2   15 Aug 1991 23:04:30   GLENN
  18.  * Forest Belt proofread/edited/cleaned up doc
  19.  * 
  20.  *    Rev 1.1   14 Jun 1991 19:52:46   GLENN
  21.  * Minor edit to file header
  22.  * 
  23.  *    Rev 1.0   07 Jun 1991 23:03:38   GLENN
  24.  * Initial revision.
  25.  *  
  26.  */
  27.  
  28.  
  29. /*  $DOC$
  30.  *  $FUNCNAME$
  31.  *     FT_RAND1()
  32.  *  $CATEGORY$
  33.  *     Math
  34.  *  $ONELINER$
  35.  *     Generate a random number
  36.  *  $SYNTAX$
  37.  *     FT_RAND1( <nMax> ) -> nRand
  38.  *  $ARGUMENTS$
  39.  *     <nMax>  Maximum limit of value to be produced.
  40.  *  $RETURNS$
  41.  *     nRand is a random number between 0 (inclusive) and <nMax> (exclusive).
  42.  *  $DESCRIPTION$
  43.  *     Generates a non-integer random number based on the Linear 
  44.  *     Congruential Method.
  45.  *
  46.  *     If you need a random number between 1 and <nMax> inclusive, INT()   
  47.  *     the result and add 1.  
  48.  *
  49.  *     If you need a random number between 0 and <nMax> inclusive, 
  50.  *     then you should ROUND() the result.
  51.  *  $EXAMPLES$
  52.  *      nResult := INT( FT_RAND1(100) ) + 1    // 1 <= nResult <= 100
  53.  *      nResult := ROUND( FT_RAND1(100), 0 )   // 0 <= nResult <= 100
  54.  *      nResult := FT_RAND1( 1 )               // 0 <= nResult < 1
  55.  *  $END$
  56.  */
  57.  
  58. #ifdef FT_TEST
  59.  
  60.   // Write 100 random numbers from 1 to 100 to stdout.
  61.   // Run it multiple times and redirect output to a file
  62.   // to check it
  63.  
  64.   function main()
  65.      local x
  66.  
  67.      for x := 1 to 100
  68.         outstd( int( ft_rand1(100) ) )
  69.         outstd( chr(13) + chr(10) )
  70.      next
  71.      return nil
  72.  
  73. #endif
  74.  
  75.  
  76. function ft_rand1(nMax)
  77.   static nSeed
  78.   local m := 100000000, b := 31415621
  79.  
  80.   nSeed := iif( nSeed == NIL, seconds(), nSeed )   // init_seed()
  81.  
  82.   return( nMax * ( ( nSeed := mod( nSeed*b+1, m ) ) / m ) )
  83.