Author Topic: Interesting article re: QBASIC random numbers  (Read 6146 times)

0 Members and 1 Guest are viewing this topic.

Offline Ed Davis

  • Newbie
  • Posts: 40
    • View Profile
Interesting article re: QBASIC random numbers
« on: January 29, 2021, 09:02:30 am »
I saw this: https://nullprogram.com/blog/2020/11/17/

Very interesting discussion on QBASIC's random number generator.  And QB64 is mentioned to!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Interesting article re: QBASIC random numbers
« Reply #1 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.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Interesting article re: QBASIC random numbers
« Reply #2 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
« Last Edit: January 29, 2021, 09:18:44 pm by Bert22306 »

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Interesting article re: QBASIC random numbers
« Reply #3 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.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Interesting article re: QBASIC random numbers
« Reply #4 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 
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline doppler

  • Forum Regular
  • Posts: 241
    • View Profile
There may never be true random.
« Reply #5 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.


Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Interesting article re: QBASIC random numbers
« Reply #6 on: January 29, 2021, 10:39:40 pm »
UUIDs.
Shuwatch!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Interesting article re: QBASIC random numbers
« Reply #7 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
« Last Edit: January 29, 2021, 11:01:14 pm by NOVARSEG »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Interesting article re: QBASIC random numbers
« Reply #8 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.
Shuwatch!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Interesting article re: QBASIC random numbers
« Reply #9 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...

...
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Interesting article re: QBASIC random numbers
« Reply #10 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/
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Interesting article re: QBASIC random numbers
« Reply #11 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
« Last Edit: January 29, 2021, 11:27:00 pm by NOVARSEG »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Interesting article re: QBASIC random numbers
« Reply #12 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
Shuwatch!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Interesting article re: QBASIC random numbers
« Reply #13 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. 
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Interesting article re: QBASIC random numbers
« Reply #14 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/