Menu

RANDOM() b0rkage?

2019-06-03
2019-06-05
  • Yet Another Troll

    In the Windows version 1.25-50d, RANDOM(n%) sometimes returns n%, when the 1.26 documentation states that it returns 0..n%-1. Do I need to fall back to RAND(0) MOD n% and possible bias? Seen in Winblows 10 and H8.1. I do not see the problem on Android 5.1.1 using the 1.25-50 APK. Thanks!

     ' Randomize
     Clr i%, j%, k%
     For i% = 0 To 499999
      j% = Random(@CLng%(666)) ! Force data typing
      If j% >= 666 Then
       Print i%, j%
       Inc k%
      Else If j% < 0 Then
       Print i%, j%
      EndIf
     Next i%
     Print k% / i%
    End
    
    ' My Word VBA macro monkey programmer/analyst wannabe-ness is showing!
    DefFn CLng%(n%) = n%
    
     

    Last edit: Yet Another Troll 2019-06-03
  • Yet Another Troll

    This appears to fix it when Max is an integer but perhaps not when Max is floating point or complex.
    Is RANDOM's behavior when Max is negative by design or accidental?

      Repeat
       j% = Random(Max%)
      Until (j% = 0) Or (j% <> Max%) ! Max% could be <= 0
    
     
  • Markus Hoffmann

    Markus Hoffmann - 2019-06-04

    If the argument (arg) to RANDOM() is negative, a random number between ]arg,0] should be returned.

     
  • Markus Hoffmann

    Markus Hoffmann - 2019-06-04

    Hm, interesting point. The Result of yout program above running under linux is 0.

    However, the WINDOWS version giles a lot of:

    38723 666
    67538 666
    70001 666
    72302 666
    76270 666
    ...

     

    Last edit: Markus Hoffmann 2019-06-04
  • Markus Hoffmann

    Markus Hoffmann - 2019-06-04

    This program shows the difference. The last line is 10 0 on linux and 10 6 on WINDOWS. So a tiny fraction of the random number is the argument.

    DIM a(10+1)
    
    for i=0 to 100000
      INC a(RANDOM(10.0))
    next i
    
    for i=0 to 10
      print i,a(i)
    next i
    quit
    

    The same problem with RND():

    DIM a(10+1)
    
    for i=0 to 100000
     ' INC a(RANDOM(10))
     INC a(INT(10*RND()))
    next i
    
    for i=0 to 10
      print i,a(i)
    next i
    quit
    
     

    Last edit: Markus Hoffmann 2019-06-04
  • Markus Hoffmann

    Markus Hoffmann - 2019-06-04

    The difference is that WINDOWS uses a 15 bit random generator only and linux a 31 bit randlom generator. That means, one out of 32768 random values will be equal to the argument of the randlom function on WINDOWS and on linux only one out of 2147483648 on linux (and Android).

     
    🎉
    1
  • Markus Hoffmann

    Markus Hoffmann - 2019-06-04

    I have addressed that issue in the manual now, since I believe I cannot really fix it in X11-Basic. Thanks for mentioning it!

     
    • Yet Another Troll

      Could this possibly affect RAND too, perhaps with an over/underflow/wraparound to 0x80000000?

       
  • Markus Hoffmann

    Markus Hoffmann - 2019-06-05

    I can think of a quick fix which prevents the value beeing the argument of RANDOM(). And yes, RAND is affected, too. However, I cannot make WINDOWS random generator have a higher resolution.

    This fix is now in 1.26-54

     
    🎉
    1

    Last edit: Markus Hoffmann 2019-06-05

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.