better RandomInt() ...

This forum is for general support of Xbase++
Post Reply
Message
Author
User avatar
Auge_Ohr
Posts: 1422
Joined: Wed Feb 24, 2010 3:44 pm

better RandomInt() ...

#1 Post by Auge_Ohr »

hi,
with this Code i got 60-65% ... but i need better Random

Code: Select all

Procedure Main
LOCAL i
LOCAL iMax := 1000
LOCAL nZahl
LOCAL nPosi
LOCAL aTreff := {}

   SET ALTER TO _LOGFILE.TXT
   SET ALTER ON

   FOR i := 1 TO iMax
      nZahl := RandomInt(1,iMax)

      nPosi := ASCAN(aTreff,{|x| x[1] = nZahl } )
      IF nPosi > 0
         aTreff[nPosi,2] := aTreff[nPosi,2] +1
      ELSE
         AADD(aTreff,{nZahl,1} )
      ENDIF
   NEXT

   ? iMax := LEN(aTreff)
   aTreff := ASort(aTreff,,, {|aX,aY| aX[2] < aY[2]} )
   FOR i := 1 TO iMax
       ? aTreff[i,1],aTreff[i,2]
   NEXT

   SET ALTER OFF
   SET ALTER TO

RETURN
i also try Alaska BaseRandom

Code: Select all

FUNCTION BaseRandom( nLimit )
STATIC snRandom := Nil
LOCAL nDecimals
LOCAL cLimit

   IF snRandom == NIL
      snRandom := SECONDS() / EXP( 1 )
   ENDIF

   snRandom := LOG( snRandom + SQRT( 2 ) ) * EXP( 3 )
   snRandom := VAL( STR( snRandom - INT( snRandom ), 17, 15 ) )
   cLimit := TRANSFORM( nLimit, "@N" )
   nDecimals := AT( ".", cLimit )

   IF nDecimals > 0
      nDecimals := LEN( cLimit ) - nDecimals
   ENDIF

RETURN ROUND( nLimit * snRandom, nDecimals )
// BaseRandom Function is example Random Function from Alaska, but
// it has a decided tendancy to avoid numbers near the edge of the range!
greetings by OHR
Jimmy

User avatar
Auge_Ohr
Posts: 1422
Joined: Wed Feb 24, 2010 3:44 pm

Re: better RandomInt() ...

#2 Post by Auge_Ohr »

found this
re : Random()
public.xbase++.clipper-migration
11. August 2005

and

Mersenne Twister PRNG
public.xbase++.clipper-migration
12. August 2005
Thomas Brown Solution does produce less Dupe than Alaska Function
RandomInt() -> 600 / 1000
Mersenne() -> 50 / 1000
greetings by OHR
Jimmy

Post Reply