none
How to write a program to generate a random number (not by using Math.GetRandomNumber()) ?? RRS feed

  • Question

  • Hi Team,

    I need a suggestion or recommendation for the following question

    How to write a Small Basic program to generate a random number (not by using Math.GetRandomNumber()) ??

    Thanks,

    Roshan

    Sunday, August 20, 2017 4:08 PM

Answers

  • I long ago wrote VBScript programs to generate pseudo random numbers. I used Multiply With Carry generators, because they had long periods and good randomness characteristics, even on 32-bit systems. I found them much better that Linear Congruential Generators. The following is an example:

    http://www.rlmueller.net/MWC32.htm

    The page shows all of the math, and a description. I assume the code linked on the page can be converted to Small Basic. But the code takes into account that VBScript can only deal with 53-bits of accuracy, so some of the techniques to avoid round off errors may not be necessary in Small Basic. As usual, the generator produces integers, in this case between 0 and 4,294,967,296 (2^32), so you may need to scale the integers for other ranges, or divide the integers by the modulus (4,294,967,296) to produce decimal values between 0 and 1.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by [Roshan_Kumar] Tuesday, August 22, 2017 11:07 AM
    Sunday, August 20, 2017 7:05 PM
  • Here's a quick conversion of Richard's MWC32.vbs to SB (with LitDev ext, for the sake of simplicity)

    ID: JFB378-0


    Monday, August 21, 2017 9:25 PM
    Answerer
  • ' Microsoft Small Basic Ver 1.2
    ' by Jan Zumwalt    2017.09.10
    ' Linear Congruential Generator
    ' recomended values https://en.wikipedia.org/wiki/Linear_congruential_generator
    '              a                           c
    '        1664525        1013904223
    '     22695477                            1
    ' 1103515245                   12345
    '         214013               2531011
    '         69069                            1
    '  16843009         826366247

    seed = Clock.ElapsedMilliseconds
    a = 1103515245
    c = 12345
    m = Math.Power(2,32)      ' = 4,294,967,296

    For x = 1 to 100
      rand()
      rnd = return
      TextWindow.WriteLine(rnd)
    endfor

    sub rand   ' returns a random number between 0 and 1
      seed = math.Remainder(a * seed + c, m) / 1000000
      return = seed - Math.floor(seed)
    EndSub
    Monday, September 11, 2017 2:15 AM

All replies

  • I long ago wrote VBScript programs to generate pseudo random numbers. I used Multiply With Carry generators, because they had long periods and good randomness characteristics, even on 32-bit systems. I found them much better that Linear Congruential Generators. The following is an example:

    http://www.rlmueller.net/MWC32.htm

    The page shows all of the math, and a description. I assume the code linked on the page can be converted to Small Basic. But the code takes into account that VBScript can only deal with 53-bits of accuracy, so some of the techniques to avoid round off errors may not be necessary in Small Basic. As usual, the generator produces integers, in this case between 0 and 4,294,967,296 (2^32), so you may need to scale the integers for other ranges, or divide the integers by the modulus (4,294,967,296) to produce decimal values between 0 and 1.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by [Roshan_Kumar] Tuesday, August 22, 2017 11:07 AM
    Sunday, August 20, 2017 7:05 PM
  • Here's a quick conversion of Richard's MWC32.vbs to SB (with LitDev ext, for the sake of simplicity)

    ID: JFB378-0


    Monday, August 21, 2017 9:25 PM
    Answerer
  • FoshanPriyaKumar,

    How about to use a digit of Clock.Millisecond property?  


    Nonki Takahashi

    Tuesday, August 22, 2017 1:16 PM
    Moderator
  • Hi

    Unmarked as answer, after looking  through your 'answers?' (there are lots of similar cases)

    Simply copying or repeating others solutions and sell as your's is rather cheap and fools others which spent more time on that, than simply a second.

    thanks.

    Tuesday, September 5, 2017 3:34 PM
    Answerer
  • ' Microsoft Small Basic Ver 1.2
    ' by Jan Zumwalt    2017.09.10
    ' Linear Congruential Generator
    ' recomended values https://en.wikipedia.org/wiki/Linear_congruential_generator
    '              a                           c
    '        1664525        1013904223
    '     22695477                            1
    ' 1103515245                   12345
    '         214013               2531011
    '         69069                            1
    '  16843009         826366247

    seed = Clock.ElapsedMilliseconds
    a = 1103515245
    c = 12345
    m = Math.Power(2,32)      ' = 4,294,967,296

    For x = 1 to 100
      rand()
      rnd = return
      TextWindow.WriteLine(rnd)
    endfor

    sub rand   ' returns a random number between 0 and 1
      seed = math.Remainder(a * seed + c, m) / 1000000
      return = seed - Math.floor(seed)
    EndSub
    Monday, September 11, 2017 2:15 AM
  • Here is another approach:

    https://xkcd.com/221/

    :-)

    • Marked as answer by [Roshan_Kumar] Monday, September 18, 2017 12:22 PM
    • Unmarked as answer by [Roshan_Kumar] Sunday, September 24, 2017 2:58 PM
    Thursday, September 14, 2017 1:54 PM
    Answerer
  • That must be a gov't program that determines how much income tax you get back!!!
    Monday, September 18, 2017 8:37 PM