Best Random Number Generator! ?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
Anthony.R.Brown
Veteran
Posts: 157
Joined: Thu Mar 27, 2014 1:03 pm

Best Random Number Generator! ?

Post 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
Mike Chambers
Newbie
Posts: 5
Joined: Sun Jul 27, 2008 5:39 pm
Location: St. Louis, MO

Re: Best Random Number Generator! ?

Post 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:

Code: Select all

initrandom
mynum = rand& MOD 256
The code will crash with overflow errors in the interpreter, but will work compiled.
-Mike
User avatar
Anthony.R.Brown
Veteran
Posts: 157
Joined: Thu Mar 27, 2014 1:03 pm

Re: Best Random Number Generator! ?

Post 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:

Code: Select all

initrandom
mynum = rand& MOD 256
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
User avatar
Anthony.R.Brown
Veteran
Posts: 157
Joined: Thu Mar 27, 2014 1:03 pm

Re: Best Random Number Generator! ?

Post by Anthony.R.Brown »

The MAX 60000 Characters Stops me Posting My Program!?? :(
User avatar
Anthony.R.Brown
Veteran
Posts: 157
Joined: Thu Mar 27, 2014 1:03 pm

Re: Best Random Number Generator! ?

Post 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.
Attachments
THERNGTESTPROGRAM.BAS.zip
(7.33 KiB) Downloaded 898 times
rockyabq
Newbie
Posts: 2
Joined: Fri Jan 23, 2015 11:32 pm

Re: Best Random Number Generator! ?

Post 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
HarryPotter14
Newbie
Posts: 9
Joined: Mon Feb 02, 2015 8:44 am

Re: Best Random Number Generator! ?

Post 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! :)
HarryPotter14
Newbie
Posts: 9
Joined: Mon Feb 02, 2015 8:44 am

Re: Best Random Number Generator! ?

Post by HarryPotter14 »

My error. Where I said:

add another constant then perform another add

I meant:

add another constant then perform another XOR

Sorry! :(
Post Reply