Guidance
指路人
g.yi.org
software / rapidq / Examples / Algorithm & Maths / random number generator.bas

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
'From: gbosmis Tue, 03 Feb 2004 12:54:55 -0000
' A random number generator with good statistical properties.


' This algorithm is from numerical recipes in c.
' It is a powerful random number generator, but not so powerful as
     Mersenne Twister :) .
' I hope the code translation from c to rapidq was made correctly.

     $INCLUDE "RAPIDQ.INC"
     $TYPECHECK ON

     DECLARE SUB CreateRand

     CONST IM1 = 2147483563
     CONST IM2 = 2147483399
     CONST AM = 1 / IM1
     CONST IMM1 = IM1-1
     CONST IA1 = 40014
     CONST IA2 = 40692
     CONST IQ1 = 53668
     CONST IQ2 = 52774
     CONST IR1 = 12211
     CONST IR2 = 3791
     CONST NTAB = 32
     CONST NDIV = (1 + IMM1 / NTAB)
     CONST EPS = 1.2e-7
     CONST RNMX = 1 - EPS

     DIM idum2 AS LONG
     idum2=123456789
     DIM iy AS LONG
     iy=0
     DIM iv(NTAB) AS LONG

' return uniform random deviate between 0 and 1 (long period > 2*10^18 !!!)
' call with idum a negative integer to initialize
' thereafter do not alter idum between succesive deviates in a sequence
     FUNCTION ran2(idum AS LONG) AS DOUBLE
      DIM j AS INTEGER
      DIM k AS LONG
      DIM temp AS DOUBLE
' initialize if the integer is negative
      IF idum <= 0 THEN
       IF (-idum) < 1 THEN
        idum = 1
       ELSE
        idum = -idum
       END IF
       idum2 = idum
       FOR j = NTAB + 7 TO 0 STEP -1
        k = idum / IQ1
        idum = IA1 * (idum - k * IQ1) - k * IR1
        IF idum < 0 THEN
         idum = idum + IM1
        END IF
        IF j < NTAB THEN
         iv(j) = idum
        END IF
       NEXT
       iy = iv(0)
      END IF
' after that just hold the seed constant
      k = idum/IQ1
      idum = IA1 * (idum - k * IQ1) - k * IR1
      IF (idum < 0) THEN idum = idum + IM1
      k = idum2 / IQ2
      idum2 = IA2 * (idum2 - k * IQ2) - k * IR2
      IF (idum2 < 0) THEN idum2 = idum2 + IM2
      j=iy/NDIV
      iy = iv(j) - idum2
      iv(j) = idum
      IF (iy < 1) THEN iy = iy + IMM1
      temp = AM * iy
      IF (temp > RNMX) THEN
       ran2 = RNMX
      ELSE
       ran2 = temp
      END IF
     END FUNCTION


' The Form

     CREATE Form AS QFORM
      CAPTION = "Random Nbrs Between 0 and 1"
      Width = 320
      Height = 240
      Center
      CREATE Label1 AS QLABEL
       CAPTION = "Seed"
       Left = 27
       Top = 15
       Transparent = 1
      END CREATE
      CREATE Button1 AS QBUTTON
       CAPTION = "Rand Nbrs"
       Left = 207
       Top = 177
       OnClick=CreateRand
      END CREATE
      CREATE Edit1 AS QEDIT
       Text = "123456"
       Left = 159
       Top = 8
       TabOrder = 9
      END CREATE
      CREATE RichEdit1 AS QRICHEDIT
       Left = 25
       Top = 44
       Width = 257
       Height = 121
       ScrollBars = 2
       TabOrder = 10
      END CREATE
     END CREATE

     Form.SHOWMODAL

' The Subs

     SUB CreateRand
      DEFDBL seed,null
      DEFINT i
      seed=-87654321
      null=ran2(seed)
      seed=VAL(Edit1.text)
      null=ran2(seed)
      FOR i=1 TO 100
       RichEdit1.addstring STR$(ran2(seed))
      NEXT i
     END SUB
掌柜推荐
 
 
¥738.00 ·
 
 
¥1,480.00 ·
 
 
¥397.00 ·
 
 
¥950.00 ·
 
 
¥264.00 ·
 
 
¥1,005.00 ·
© Sun 2024-11-24  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2009-07-25 17:30:54