home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l180 / 2.ddi / RANDOMS.BAS < prev    next >
Encoding:
BASIC Source File  |  1989-02-07  |  10.4 KB  |  317 lines

  1.   ' ************************************************
  2.   ' **  Name:          RANDOMS                    **
  3.   ' **  Type:          Toolbox                    **
  4.   ' **  Module:        RANDOMS.BAS                **
  5.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  6.   ' ************************************************
  7.   ' USAGE:           No command line parameters
  8.   ' .MAK FILE:       (none)
  9.   ' PARAMETERS:      (none)
  10.   ' VARIABLES:       i%      Loop index for generating pseudorandom numbers
  11.   
  12.     DECLARE FUNCTION Rand& ()
  13.     DECLARE FUNCTION RandExponential! (mean!)
  14.     DECLARE FUNCTION RandFrac! ()
  15.     DECLARE FUNCTION RandInteger% (a%, b%)
  16.     DECLARE FUNCTION RandNormal! (mean!, stddev!)
  17.     DECLARE FUNCTION RandReal! (x!, y!)
  18.   
  19.     DECLARE SUB RandShuffle (key$)
  20.   
  21.   ' Array of long integers for generating all randoms
  22.     DIM SHARED r&(1 TO 100)
  23.   
  24.   ' Clear the screen
  25.     CLS
  26.   
  27.   ' Shuffle the random number generator, creating a
  28.   ' unique sequence for every possible second
  29.     RandShuffle DATE$ + TIME$
  30.   
  31.     PRINT "Rand&"
  32.     FOR i% = 1 TO 5
  33.         PRINT Rand&,
  34.     NEXT i%
  35.     PRINT
  36.   
  37.     PRINT "RandInteger%(0, 9)"
  38.     FOR i% = 1 TO 5
  39.         PRINT RandInteger%(0, 9),
  40.     NEXT i%
  41.     PRINT
  42.   
  43.     PRINT "RandReal!(-10!, 10!)"
  44.     FOR i% = 1 TO 5
  45.         PRINT RandReal!(-10!, 10!),
  46.     NEXT i%
  47.     PRINT
  48.   
  49.     PRINT "RandExponential!(100!)"
  50.     FOR i% = 1 TO 5
  51.         PRINT RandExponential!(100!),
  52.     NEXT i%
  53.     PRINT
  54.   
  55.     PRINT "RandNormal!(100!, 10!)"
  56.     FOR i% = 1 TO 5
  57.         PRINT RandNormal!(100!, 10!),
  58.     NEXT i%
  59.     PRINT
  60.   
  61.     PRINT "RandFrac!"
  62.     FOR i% = 1 TO 5
  63.         PRINT RandFrac!,
  64.     NEXT i%
  65.     PRINT
  66.   
  67.   ' ************************************************
  68.   ' **  Name:          Rand&                      **
  69.   ' **  Type:          Function                   **
  70.   ' **  Module:        RANDOMS.BAS                **
  71.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  72.   ' ************************************************
  73.   '
  74.   ' Returns a pseudorandom long integer in the range
  75.   ' 0 through 999999999.
  76.   '
  77.   ' EXAMPLE OF USE:  n& = Rand&
  78.   ' PARAMETERS:      (none)
  79.   ' VARIABLES:       i%         First index into random number table
  80.   '                  j%         Second index into random number table
  81.   '                  t&         Working variable
  82.   ' MODULE LEVEL
  83.   '   DECLARATIONS:  DECLARE FUNCTION Rand& ()
  84.   '                  DIM SHARED r&(1 TO 100)
  85.   '
  86.     FUNCTION Rand& STATIC
  87.       
  88.       ' Get the pointers into the table
  89.         i% = r&(98)
  90.         j% = r&(99)
  91.       
  92.       ' Subtract the two table values
  93.         t& = r&(i%) - r&(j%)
  94.       
  95.       ' Adjust result if less than zero
  96.         IF t& < 0 THEN
  97.             t& = t& + 1000000000
  98.         END IF
  99.       
  100.       ' Replace table entry with new random number
  101.         r&(i%) = t&
  102.       
  103.       ' Decrement first index, keeping in range 1 through 55
  104.         IF i% > 1 THEN
  105.             r&(98) = i% - 1
  106.         ELSE
  107.             r&(98) = 55
  108.         END IF
  109.       
  110.       ' Decrement second index, keeping in range 1 through 55
  111.         IF j% > 1 THEN
  112.             r&(99) = j% - 1
  113.         ELSE
  114.             r&(99) = 55
  115.         END IF
  116.       
  117.       ' Use last random number to index into shuffle table
  118.         i% = r&(100) MOD 42 + 56
  119.       
  120.       ' Grab random from table as current random number
  121.         r&(100) = r&(i%)
  122.       
  123.       ' Put new calculated random into table
  124.         r&(i%) = t&
  125.       
  126.       ' Return the random number grabbed from the table
  127.         Rand& = r&(100)
  128.       
  129.     END FUNCTION
  130.   
  131.   ' ************************************************
  132.   ' **  Name:          RandExponential!           **
  133.   ' **  Type:          Function                   **
  134.   ' **  Module:        RANDOMS.BAS                **
  135.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  136.   ' ************************************************
  137.   '
  138.   ' Returns an exponentially distributed pseudorandom,
  139.   ' single-precision number given the mean of the
  140.   ' distribution.
  141.   '
  142.   ' EXAMPLE OF USE:  x! = RandExponential!(mean!)
  143.   ' PARAMETERS:      mean!          The mean of the exponential distribution
  144.   ' VARIABLES:       (none)
  145.   ' MODULE LEVEL
  146.   '   DECLARATIONS:  DECLARE FUNCTION RandExponential! (mean!)
  147.   '
  148.     FUNCTION RandExponential! (mean!) STATIC
  149.         RandExponential! = -mean! * LOG(RandFrac!)
  150.     END FUNCTION
  151.   
  152.   ' ************************************************
  153.   ' **  Name:          RandFrac!                  **
  154.   ' **  Type:          Function                   **
  155.   ' **  Module:        RANDOMS.BAS                **
  156.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  157.   ' ************************************************
  158.   '
  159.   ' Returns a pseudorandom, single-precision number
  160.   ' in the range 0 through 1.
  161.   '
  162.   ' EXAMPLE OF USE:  x! = RandFrac!
  163.   ' PARAMETERS:      (none)
  164.   ' VARIABLES:       (none)
  165.   ' MODULE LEVEL
  166.   '   DECLARATIONS:  DECLARE FUNCTION RandFrac! ()
  167.   '
  168.     FUNCTION RandFrac! STATIC
  169.         RandFrac! = Rand& / 1E+09
  170.     END FUNCTION
  171.   
  172.   ' ************************************************
  173.   ' **  Name:          RandInteger%               **
  174.   ' **  Type:          Function                   **
  175.   ' **  Module:        RANDOMS.BAS                **
  176.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  177.   ' ************************************************
  178.   '
  179.   ' Returns a pseudorandom integer in the range
  180.   ' a% to b% inclusive.
  181.   '
  182.   ' EXAMPLE OF USE:  n% = RandInteger%(a%, b%)
  183.   ' PARAMETERS:      a%           Minimum value for returned integer
  184.   '                  b%           Maximum value for returned integer
  185.   ' VARIABLES:       (none)
  186.   ' MODULE LEVEL
  187.   '   DECLARATIONS:  DECLARE FUNCTION RandInteger% (a%, b%)
  188.   '
  189.     FUNCTION RandInteger% (a%, b%) STATIC
  190.         RandInteger% = a% + (Rand& MOD (b% - a% + 1))
  191.     END FUNCTION
  192.   
  193.   ' ************************************************
  194.   ' **  Name:          RandNormal!                **
  195.   ' **  Type:          Function                   **
  196.   ' **  Module:        RANDOMS.BAS                **
  197.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  198.   ' ************************************************
  199.   '
  200.   ' Returns a normally distributed single-precision,
  201.   ' pseudorandom number given the mean and standard deviation.
  202.   '
  203.   ' EXAMPLE OF USE:  x! = RandNormal!(mean!, stddev!)
  204.   ' PARAMETERS:      mean!           Mean of the distribution of returned
  205.   '                                  values
  206.   '                  stddev!         Standard deviation of the distribution
  207.   ' VARIABLES:       u1!             Pseudorandom positive real value
  208.   '                                  less than 1
  209.   '                  u2!             Pseudorandom positive real value
  210.   '                                  less than 1
  211.   '                  x!              Working value
  212.   ' MODULE LEVEL
  213.   '   DECLARATIONS:  DECLARE FUNCTION RandNormal! (mean!, stddev!)
  214.   '
  215.     FUNCTION RandNormal! (mean!, stddev!) STATIC
  216.         u1! = RandFrac!
  217.         u2! = RandFrac!
  218.         x! = SQR(-2! * LOG(u1!)) * COS(6.283185 * u2)
  219.         RandNormal! = mean! + stddev! * x!
  220.     END FUNCTION
  221.   
  222.   ' ************************************************
  223.   ' **  Name:          RandReal!                  **
  224.   ' **  Type:          Function                   **
  225.   ' **  Module:        RANDOMS.BAS                **
  226.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  227.   ' ************************************************
  228.   '
  229.   ' Returns a pseudorandom, single-precision real
  230.   ' number in the range x! to y!.
  231.   ' EXAMPLE OF USE:  z! = RandReal!(x!, y!)
  232.   ' PARAMETERS:      x!           Minimum for returned value
  233.   '                  y!           Maximum for returned value
  234.   ' VARIABLES:       (none)
  235.   ' MODULE LEVEL
  236.   '   DECLARATIONS:  DECLARE FUNCTION RandReal! (x!, y!)
  237.   '
  238.     FUNCTION RandReal! (x!, y!) STATIC
  239.         RandReal! = x! + (y! - x!) * (Rand& / 1E+09)
  240.     END FUNCTION
  241.   
  242.   ' ************************************************
  243.   ' **  Name:          RandShuffle                **
  244.   ' **  Type:          Subprogram                 **
  245.   ' **  Module:        RANDOMS.BAS                **
  246.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  247.   ' ************************************************
  248.   '
  249.   ' Creates original table of pseudorandom long integers
  250.   ' for use by the function Rand&.  The contents of key$
  251.   ' are used to seed the table.
  252.   '
  253.   ' EXAMPLE OF USE:  RandShuffle(key$)
  254.   ' PARAMETERS:      key$            String used to seed the generator
  255.   '          r&(1 TO 100) (shared)   Array of long integers for
  256.   '                                  generating pseudorandom numbers
  257.   ' VARIABLES:       k$              Modified key string
  258.   '                  i%              Index into k$, index into table
  259.   '                  j%              Index into table
  260.   '                  k%              Loop count for warming up generator
  261.   ' MODULE LEVEL
  262.   '   DECLARATIONS:  DECLARE SUB RandShuffle (key$)
  263.   '
  264.     SUB RandShuffle (key$) STATIC
  265.       
  266.       ' Form 97-character string, with key$ as part of it
  267.         k$ = LEFT$("Abra Ca Da Bra" + key$ + SPACE$(83), 97)
  268.       
  269.       ' Use each character to seed table
  270.         FOR i% = 1 TO 97
  271.             r&(i%) = ASC(MID$(k$, i%, 1)) * 8171717 + i% * 997&
  272.         NEXT i%
  273.       
  274.       ' Preserve string space
  275.         k$ = ""
  276.       
  277.       ' Initialize pointers into table
  278.         i% = 97
  279.         j% = 12
  280.       
  281.       ' Randomize the table to get it warmed up
  282.         FOR k% = 1 TO 997
  283.           
  284.           ' Subtract entries pointed to by i% and j%
  285.             r&(i%) = r&(i%) - r&(j%)
  286.           
  287.           ' Adjust result if less than zero
  288.             IF r&(i%) < 0 THEN
  289.                 r&(i%) = r&(i%) + 1000000000
  290.             END IF
  291.           
  292.           ' Decrement first index, keeping in range of 1 through 97
  293.             IF i% > 1 THEN
  294.                 i% = i% - 1
  295.             ELSE
  296.                 i% = 97
  297.             END IF
  298.           
  299.           ' Decrement second index, keeping in range of 1 through 97
  300.             IF j% > 1 THEN
  301.                 j% = j% - 1
  302.             ELSE
  303.                 j% = 97
  304.             END IF
  305.           
  306.         NEXT k%
  307.       
  308.       ' Initialize pointers for use by Rand& function
  309.         r&(98) = 55
  310.         r&(99) = 24
  311.       
  312.       ' Initialize pointer for shuffle table lookup by Rand& function
  313.         r&(100) = 77
  314.       
  315.     END SUB
  316.   
  317.