Answered by:
How to write a program to generate a random number (not by using Math.GetRandomNumber()) ??
Question

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 32bit 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 53bits 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

Here's a quick conversion of Richard's MWC32.vbs to SB (with LitDev ext, for the sake of simplicity)
ID: JFB3780
 Edited by Pappa LapubEditor Monday, August 21, 2017 9:36 PM
 Marked as answer by [Roshan_Kumar] Tuesday, August 22, 2017 11:07 AM

' 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 Marked as answer by Pappa LapubEditor Monday, September 11, 2017 2:58 PM
 Unmarked as answer by [Roshan_Kumar] Monday, September 11, 2017 3:13 PM
 Marked as answer by [Roshan_Kumar] Monday, September 11, 2017 3:13 PM
 Edited by jwzumwalt1 Monday, September 11, 2017 4:33 PM
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 32bit 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 53bits 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

Here's a quick conversion of Richard's MWC32.vbs to SB (with LitDev ext, for the sake of simplicity)
ID: JFB3780
 Edited by Pappa LapubEditor Monday, August 21, 2017 9:36 PM
 Marked as answer by [Roshan_Kumar] Tuesday, August 22, 2017 11:07 AM


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.

' 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 Marked as answer by Pappa LapubEditor Monday, September 11, 2017 2:58 PM
 Unmarked as answer by [Roshan_Kumar] Monday, September 11, 2017 3:13 PM
 Marked as answer by [Roshan_Kumar] Monday, September 11, 2017 3:13 PM
 Edited by jwzumwalt1 Monday, September 11, 2017 4:33 PM

 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
