Page 1 of 1
Best Random Number Generator! ?
Posted: Tue Apr 01, 2014 7:06 am
by Anthony.R.Brown
Hi...
Does anyone know what the Best Random Number Generator is ?
As an Example how can the Simple one below be improved!
RANDOMIZE TIMER
RNDTEST = INT(RND * 1000) + 1
IF (RNDTEST < 500) THEN RESULT = RNDTEST
IF (RNDTEST => 500) THEN RESULT = RNDTEST
PRINT : PRINT
PRINT RESULT
Re: Best Random Number Generator! ?
Posted: Fri Apr 04, 2014 7:50 pm
by Mike Chambers
Here's one that doesn't use the built-in RND generator. It's based on the XOR shift method.
Code: Select all
DECLARE SUB initrandom ()
DECLARE FUNCTION rand& ()
DIM SHARED x AS LONG, y AS LONG, z AS LONG, w AS LONG
SUB initrandom
x = 123456789
y = 362436069
z = 521288629
w = 88675123
END SUB
FUNCTION rand&
DIM t AS LONG
t = x XOR (x * 2048&)
x = y: y = z: z = w
w = w XOR (w \ 524288) XOR (t XOR (t \ 256&))
rand& = w
END FUNCTION
There are better algorithms, but this is pretty solid. You can limit it's range with a modulus operation, like lets say you want to keep it inside 0 to 255:
The code will crash with overflow errors in the interpreter, but will work compiled.
Re: Best Random Number Generator! ?
Posted: Sat May 31, 2014 11:20 am
by Anthony.R.Brown
Mike Chambers wrote:Here's one that doesn't use the built-in RND generator. It's based on the XOR shift method.
Code: Select all
DECLARE SUB initrandom ()
DECLARE FUNCTION rand& ()
DIM SHARED x AS LONG, y AS LONG, z AS LONG, w AS LONG
SUB initrandom
x = 123456789
y = 362436069
z = 521288629
w = 88675123
END SUB
FUNCTION rand&
DIM t AS LONG
t = x XOR (x * 2048&)
x = y: y = z: z = w
w = w XOR (w \ 524288) XOR (t XOR (t \ 256&))
rand& = w
END FUNCTION
There are better algorithms, but this is pretty solid. You can limit it's range with a modulus operation, like lets say you want to keep it inside 0 to 255:
The code will crash with overflow errors in the interpreter, but will work compiled.
Hi Mike Chambers
Nice! (RNG) But!
I have to let you & Others know I have Developed the ultimate (RNG)...
and that's exactly what it is Called! the ULTIMATE(RNG) all will be Revealed Once the QB64 Site is Back on line! ? the Site has been down for Days! ?
A.R.B
Re: Best Random Number Generator! ?
Posted: Tue Jun 03, 2014 3:12 pm
by Anthony.R.Brown
The MAX 60000 Characters Stops me Posting My Program!??
Re: Best Random Number Generator! ?
Posted: Tue Jun 03, 2014 3:13 pm
by Anthony.R.Brown
Anthony.R.Brown wrote:The MAX 60000 Characters Stops me Posting My Program!??
So My RNG Test Program + ULTIMATE(RNG) are in the .zip file attached! Enjoy
Anthony.
Re: Best Random Number Generator! ?
Posted: Sat Jan 24, 2015 5:13 pm
by rockyabq
Hi Mike,
I'm afraid I'm not understanding your random number generator. I tried to pare it down but I can only get it to generate the exact same number over and over. While I assume that the number is a "random" number, how can it generate a string of
different "random" numbers?
Ideally, I need something like your method (that either bypasses or enhances the RND function) to randomly (or pseudo-randomly) generate many millions of digits that are either zeroes or ones.
Please see my question here
viewtopic.php?f=1&t=3940#p23309
and tell me if this makes sense to you.
Thanks,
Rocky
Re: Best Random Number Generator! ?
Posted: Fri Feb 27, 2015 6:59 pm
by HarryPotter14
Try this: (not actual code but p-code)
---------------
* read number passed to the RND function
* add then XOR to the number a constant
* add current seed
* add a constant then XOR another constant
* write the new seed
* add another constant then perform another add
* if possible, rotate the bits in the number either left or right one bit
* return the final result
----------------------------------
I have some notes about this method:
* The success of this routine depends on the constants chosen. Some groups of constants will work better than others.
* Factoring in an ever-changing number will greatly increase the "randomness" of the routine.
Tell me what you think!
Re: Best Random Number Generator! ?
Posted: Fri Feb 27, 2015 7:03 pm
by HarryPotter14
My error. Where I said:
add another constant then perform another add
I meant:
add another constant then perform another
XOR
Sorry!