QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Ed Davis on January 29, 2021, 09:02:30 am

Title: Interesting article re: QBASIC random numbers
Post by: Ed Davis on January 29, 2021, 09:02:30 am
I saw this: https://nullprogram.com/blog/2020/11/17/ (https://nullprogram.com/blog/2020/11/17/)

Very interesting discussion on QBASIC's random number generator.  And QB64 is mentioned to!
Title: Re: Interesting article re: QBASIC random numbers
Post by: SMcNeill on January 29, 2021, 09:53:39 am
https://www.qb64.org/forum/index.php?topic=1414.msg105988#msg105988

You can also find information on the process via the link above.  ;)
Title: Re: Interesting article re: QBASIC random numbers
Post by: Bert22306 on January 29, 2021, 09:12:54 pm
Geez, kinda harsh, that article, no? I have a test program here, the first one shown, which draws dots on a flat surface. Much as the article showed. But, see for yourself. The results aren't bad at all. The second PRNG test program, instead, shows the distribution of values, and min, max, and mean. Also not half bad. (In both programs, press <esc> to quit.)

Code: [Select]
REM  Program creates dots using random x and y coordinates
REM -------
DO
    CALL Initialize
    CALL DrawDots
LOOP
END

SUB Initialize
    _TITLE "Random Number Distribution"
    SCREEN _NEWIMAGE(840, 480, 12)
    CLS
END SUB

SUB DrawDots
    RANDOMIZE TIMER
    COLOR 9
    PRINT "First random number this cycle"; RND
    DO
        x = RND * 840
        y = RND * 480
        CIRCLE (x, y), 1, 14
        _DELAY .005
        IF INKEY$ = CHR$(27) THEN EXIT DO
    LOOP
    CLS
    LOCATE 15, 35
    INPUT "Start with new seed (y/n)"; cont$
    IF cont$ = "n" THEN END
END SUB


This one draws a probability distribution chart.

Code: [Select]
REM                    RANDOM NUMBER GENERATOR TEST
REM
REM  Generates a series of random numbers. Provides instantaneous value,
REM  running mean, and distribution in 0 to 0.1, 0.1 to 0.2, ... , 0.9 to
REM  1.0 groups. The minimum and maximum random numbers generated  are
REM  listed.
REM
REM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DIM x(4000), xRunMean(4000), norm(4000, 10), total(10)
DO
    REM -------
    REM  Zero out all bars and variables for new random number test.
    REM -------
    CALL zeroset(x(), xRunMean(), norm(), xMax, xMin, xTot, total())
    CALL labels
    REM -------
    REM  Now set new random number seed and start random number test, through
    REM  4000 tries. Then loop back to start.
    REM -------
    CALL RandomTest(x(), xRunMean(), norm(), xMax, xMin, xTot, total())
LOOP
END

SUB zeroset (x(), xRunMean(), norm(), xMax, xMin, xTot, total())
    _TITLE "Random Number Generator Test"
    SCREEN _NEWIMAGE(840, 480, 12)
    LINE (95, 400)-(100, 0), 0, BF
    LINE (120, 400)-(125, 0), 0, BF
    FOR j = 1 TO 10
        LINE (220 + 55 * (j - 1), 400)-(225 + 55 * (j - 1), 0), 0, BF
    NEXT j
    FOR i = 1 TO 4000
        x(i) = 0
        xRunMean(i) = 0
        FOR j = 1 TO 10
            norm(i, j) = 0
            total(j) = 0
        NEXT j
    NEXT i
    xMax = 0
    xMin = 1
    xTot = 0
END SUB

SUB labels
    CLS
    SCREEN _NEWIMAGE(840, 480, 12)
    COLOR 9
    LOCATE 1, 1
    PRINT "1.0"
    LOCATE 14, 1
    PRINT "0.5"
    LOCATE 20, 1
    PRINT "0.25"
    LOCATE 24, 1
    PRINT "0.1"
    LOCATE 26, 1
    PRINT "0.0"
    LOCATE 21, 7
    PRINT "Inst."
    LOCATE 22, 7
    PRINT "Value"
    LOCATE 21, 17
    PRINT "Running"
    LOCATE 22, 17
    PRINT "Mean"
    LOCATE 22, 30
    PRINT "<.1"
    LOCATE 22, 37
    PRINT "<.2"
    LOCATE 22, 44
    PRINT "<.3"
    LOCATE 22, 50
    PRINT "<.4"
    LOCATE 22, 57
    PRINT "<.5"
    LOCATE 22, 64
    PRINT "<.6"
    LOCATE 22, 71
    PRINT "<.7"
    LOCATE 22, 77
    PRINT "<.8"
    LOCATE 22, 85
    PRINT "<.9"
    LOCATE 22, 91
    PRINT "<1.0"
    LOCATE 27, 5
    PRINT "Sample#   Running Mean"
    LOCATE 27, 40
    COLOR 9
    PRINT "Min Value  Max Value"
END SUB

SUB RandomTest (x(), xRunMean(), norm(), xMax, xMin, xTot, total())
    pi = 3.1415926535897936
    myseed = 1E10 * (1 / (pi - 3))
    REM myseed = TIMER
    RANDOMIZE myseed
    FOR i = 1 TO 4000
        x(i) = RND
    NEXT i
    FOR i = 1 TO 4000
        LOCATE 2, 36
        COLOR 9
        PRINT "DISTRIBUTION OF RANDOM NUMBERS"
        IF x(i) > xMax THEN xMax = x(i)
        IF x(i) < xMin THEN xMin = x(i)
        xTot = xTot + x(i)
        xRunMean(i) = xTot / i
        IF x(i) < .1 THEN total(1) = total(1) + 1
        IF x(i) >= .1 AND x(i) < .2 THEN total(2) = total(2) + 1
        IF x(i) >= .2 AND x(i) < .3 THEN total(3) = total(3) + 1
        IF x(i) >= .3 AND x(i) < .4 THEN total(4) = total(4) + 1
        IF x(i) >= .4 AND x(i) < .5 THEN total(5) = total(5) + 1
        IF x(i) >= .5 AND x(i) < .6 THEN total(6) = total(6) + 1
        IF x(i) >= .6 AND x(i) < .7 THEN total(7) = total(7) + 1
        IF x(i) >= .7 AND x(i) < .8 THEN total(8) = total(8) + 1
        IF x(i) >= .8 AND x(i) < .9 THEN total(9) = total(9) + 1
        IF x(i) >= .9 THEN total(10) = total(10) + 1
        FOR j = 1 TO 10
            norm(i, j) = total(j) / i
        NEXT j
        LINE (95, 400)-(100, 400 - x(i - 1) * 400), 0, BF
        LINE (95, 400)-(100, 400 - x(i) * 400), 12, BF
        LINE (120, 400)-(125, 400 - xRunMean(i - 1) * 400), 0, BF
        LINE (120, 400)-(125, 400 - xRunMean(i) * 400), 11, BF
        FOR j = 1 TO 10
            LINE (220 + 55 * (j - 1), 400)-(225 + 55 * (j - 1), 400 - norm(i - 1, j) * 400), 0, BF
            LINE (220 + 55 * (j - 1), 400)-(225 + 55 * (j - 1), 400 - norm(i, j) * 400), 13, BF
        NEXT j
        LINE (1, 1)-(840, 1), 7
        LINE (1, 200)-(840, 200), 7
        LINE (1, 300)-(840, 300), 7
        LINE (1, 360)-(840, 360), 7
        LINE (1, 400)-(840, 400), 7
        LOCATE 4, 67
        PRINT "RNG seed ="; myseed
        LOCATE 28, 5
        COLOR 14
        PRINT USING "#####"; i
        LOCATE 28, 10
        PRINT USING "######.#####"; xRunMean(i)
        LOCATE 28, 40
        PRINT USING "###.#####"; xMin; xMax
        _DELAY .05
        IF INKEY$ = CHR$(27) THEN END
    NEXT i
END SUB
Title: Re: Interesting article re: QBASIC random numbers
Post by: Bert22306 on January 29, 2021, 09:31:19 pm
Aha, I see my same two test programs buried in there, in the old thread Steve posted. Don't know if I modified them at all, since then. Hadn't recompiled either one in a very long time.
Title: Re: Interesting article re: QBASIC random numbers
Post by: Pete on January 29, 2021, 09:56:25 pm
It's always cracked me up when people look for "randomness" from a machine built to handle conditions. Besides, every random occurrence has a predetermined outcome. If you are knowledgeable and quick enough, you can figure it out beforehand. That's my take on it. Any chance I'm wrong?

Pete 
Title: There may never be true random.
Post by: doppler on January 29, 2021, 10:10:43 pm
The search for true random is on going and may never be accomplished.  There is physical/electrical/cosmic.  Yes they even tried cosmic measurements of neutrons and particles.  Given time seemingly random stuff ain't.  There is always a repeat somewhere.  When the repeat is found and it's structure is analyzed.  It can be predicted when it will happen again.  Exactly as predicted.  So for now the best form of random is cosmic.  To make a computer be random, you are going to need to hook it up to a cosmic detector.  That won't come cheap.  Of course DARPA has one, why not you payed for it.

Title: Re: Interesting article re: QBASIC random numbers
Post by: SpriggsySpriggs on January 29, 2021, 10:39:40 pm
UUIDs.
Title: Re: Interesting article re: QBASIC random numbers
Post by: NOVARSEG on January 29, 2021, 10:48:56 pm

Take the output from the generator and feed it into the "D" input of a 4013 FLIP FLOP circuit.

Feed the "clock" input of the 4013 with a stable frequency of , say, 10khz

The 4013 will output a TRULY RANDOM series of 1s and 0s which can be fed into a computer port etc.

white noise generator
http://circuits-diy.com/wp-content/uploads/2020/01/White-Noise-Generator-Circuit.png
Title: Re: Interesting article re: QBASIC random numbers
Post by: SpriggsySpriggs on January 29, 2021, 11:16:37 pm
Just use Version 4 UUIDs. They can be guaranteed to be unique. No need for ridiculous algorithms or clocks. Easily done in Windows, Mac, and Linux quite quickly.
Title: Re: Interesting article re: QBASIC random numbers
Post by: Pete on January 29, 2021, 11:19:14 pm
Help. I'm stuck in a time loop, and can't get...

Help. I'm stuck in a time loop, and can't get...

Help. I'm stuck in a time loop, and can't get...

Help. I'm stuck in a time loop, and can't get...

...
Title: Re: Interesting article re: QBASIC random numbers
Post by: SMcNeill on January 29, 2021, 11:25:36 pm
Just use Version 4 UUIDs. They can be guaranteed to be unique. No need for ridiculous algorithms or clocks. Easily done in Windows, Mac, and Linux quite quickly.

Even easier — just get the randomness from someone else via an API.  https://www.random.org/clients/http/
Title: Re: Interesting article re: QBASIC random numbers
Post by: NOVARSEG on January 29, 2021, 11:25:50 pm
from
https://en.wikipedia.org/wiki/Hardware_random_number_generator


Quote
A hardware random number generator typically consists of a transducer to convert some aspect of the physical phenomena to an electrical signal, an amplifier and other electronic circuitry to increase the amplitude of the random fluctuations to a measurable level, and some type of analog-to-digital converter to convert the output into a digital number, often a simple binary digit 0 or 1. By repeatedly sampling the randomly varying signal, a series of random numbers is obtained.

https://www.amazon.ca/TrueRNG-V3-Hardware-Random-Generator/dp/B01KR2JHTA
Title: Re: Interesting article re: QBASIC random numbers
Post by: SpriggsySpriggs on January 29, 2021, 11:27:06 pm
Even easier — just get the randomness from someone else via an API.  https://www.random.org/clients/http/
That's.... Not easier. Nor quicker
Title: Re: Interesting article re: QBASIC random numbers
Post by: SMcNeill on January 29, 2021, 11:32:29 pm
That's.... Not easier. Nor quicker

I don’t see how it’s not easy, nor quick.

Say you want a 52 card shuffle.  It’s as simple as: https://www.random.org/sequences/?min=1&max=52&col=1&format=plain&rnd=new

Send that, read the result. 
Title: Re: Interesting article re: QBASIC random numbers
Post by: Pete on January 29, 2021, 11:37:46 pm
I do find it funny when people, who make apps like guess a number between 1 and 5, post they want the kind of randomness associated with atomic decay before they code their friggin program.

Pete
Title: Re: Interesting article re: QBASIC random numbers
Post by: NOVARSEG on January 29, 2021, 11:39:15 pm
The diode   the diode

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQSkC_qHin5FsDCf_B7EDclJbkBwVgSpzPvhrDD-mdrwBi4YjG61CDcgpI_fCH4Vo-bDwAVdyg&usqp=CAc
Title: Re: Interesting article re: QBASIC random numbers
Post by: SpriggsySpriggs on January 29, 2021, 11:40:47 pm
I don’t see how it’s not easy, nor quick.

Say you want a 52 card shuffle.  It’s as simple as: https://www.random.org/sequences/?min=1&max=52&col=1&format=plain&rnd=new

Send that, read the result.

The timing. You are now depending on a website to give you your data. You are depending on a quick internet connection. You are depending on them being available. You are depending on them not limiting your request speed. You are going to need to use an HTTP function to get the link. Version 4 UUIDs use either WinAPI for Windows or the terminal for Linux and Mac. All Local. No outside dependence.
Title: Re: Interesting article re: QBASIC random numbers
Post by: NOVARSEG on January 29, 2021, 11:44:35 pm
internet connection? API ?

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQSkC_qHin5FsDCf_B7EDclJbkBwVgSpzPvhrDD-mdrwBi4YjG61CDcgpI_fCH4Vo-bDwAVdyg&usqp=CAc

Title: Re: Interesting article re: QBASIC random numbers
Post by: luke on January 29, 2021, 11:46:01 pm
Geez, kinda harsh, that article, no?
The complaints are perfectly reasonable and correct.
Title: Re: Interesting article re: QBASIC random numbers
Post by: SpriggsySpriggs on January 29, 2021, 11:49:00 pm
internet connection? API ?

Don't worry about it. Have fun, NOVARSEG
Title: Re: Interesting article re: QBASIC random numbers
Post by: doppler on January 31, 2021, 02:42:58 pm
The diode

Everyone thought that was going to be the answer.  Until a equilibrium is achieved between voltage and heat.  After a very, very long time it starts to develop patterns of output.  Not very random after all.

You do realize to search for true random does not stop with a method to get it.  You have to prove it too.  What is really not well know is, who is  doing the most funding looking for a perfect random generator.  The Casino's.  From what I last heard, "The diode method" is being used in digital betting games.
Title: Re: Interesting article re: QBASIC random numbers
Post by: NOVARSEG on February 01, 2021, 03:11:32 am
The diode generators are as random as anything else. A LOTTO machine uses balls that tumble etc
Title: Re: Interesting article re: QBASIC random numbers
Post by: doppler on February 01, 2021, 09:01:22 am
A LOTTO machine uses balls that tumble etc
Funny you should mention the tumbling balls.  Ever wonder why "During a live lotto event."  They always say the machines have been monitored for a half hour before the pull?  There was a lottery scandal involving the some lotto officials "wetting" all balls except the a few.  The wetter balls being heavier would not pop to the top for daily numbers (3 and 4 ball type pulls).  Very smart gamble.  The balls would dry out by the time they were checked again.

Back to casino's again.  All dice are weighed/balanced/spun on all axis/sized.  Before they are shipped and again at the casino retested.

True random is the holy grail of science.  BTW, the oldest set of dice ever found were tested.  Wanna guess what they found...... "loaded".