Best Random Number Generator! ?
- Anthony.R.Brown
- Veteran
- Posts: 157
- Joined: Thu Mar 27, 2014 1:03 pm
Best Random Number Generator! ?
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
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
-
- Newbie
- Posts: 5
- Joined: Sun Jul 27, 2008 5:39 pm
- Location: St. Louis, MO
Re: Best Random Number Generator! ?
Here's one that doesn't use the built-in RND generator. It's based on the XOR shift method.
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.
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
Code: Select all
initrandom
mynum = rand& MOD 256
-Mike
- Anthony.R.Brown
- Veteran
- Posts: 157
- Joined: Thu Mar 27, 2014 1:03 pm
Re: Best Random Number Generator! ?
Mike Chambers wrote:Here's one that doesn't use the built-in RND generator. It's based on the XOR shift method.
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: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
The code will crash with overflow errors in the interpreter, but will work compiled.Code: Select all
initrandom mynum = rand& MOD 256
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
- Anthony.R.Brown
- Veteran
- Posts: 157
- Joined: Thu Mar 27, 2014 1:03 pm
Re: Best Random Number Generator! ?
The MAX 60000 Characters Stops me Posting My Program!??
- Anthony.R.Brown
- Veteran
- Posts: 157
- Joined: Thu Mar 27, 2014 1:03 pm
Re: Best Random Number Generator! ?
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.
- Attachments
-
- THERNGTESTPROGRAM.BAS.zip
- (7.33 KiB) Downloaded 918 times
Re: Best Random Number Generator! ?
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
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
-
- Newbie
- Posts: 9
- Joined: Mon Feb 02, 2015 8:44 am
Re: Best Random Number Generator! ?
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!
---------------
* 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!
-
- Newbie
- Posts: 9
- Joined: Mon Feb 02, 2015 8:44 am
Re: Best Random Number Generator! ?
My error. Where I said:
add another constant then perform another add
I meant:
add another constant then perform another XOR
Sorry!
add another constant then perform another add
I meant:
add another constant then perform another XOR
Sorry!