Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting a random number wihout RND.
#1
A while back I found some TI-84 code for a random number generator.  Played with it for a while, adapting it to QB64.  Messed with the calcs.  Gave it more randomness (I think) by feeding it the starting x/y position of the QB84 windows that seems to be placed randomly, and threw in TIMER for good luck.  I don't really understand it, but here it is for anyone interested in such a function.

- Dav

Code: (Select All)
'========
'RAND.BAS
'========
'Get a random number, without using RND
'Adapted for QB64 from TI-84 source found online.
'Thrown together By Dav, SEP/2023

'Since the QB64 window x/y position always starts at a
'random position, I'm using that as the seed starter.

_Delay .25

DIM SHARED seed: seed = _SCREENX * _SCREENY * TIMER

SCREEN _NEWIMAGE(800, 600, 32)

'Blocks of random size of random colors at random positions.

DO
    x = Rand(_WIDTH)
    y = Rand(_HEIGHT)
    s = Rand(100)
    r = Rand(255): g = Rand(255): b = Rand(255)
    LINE (x, y)-STEP(s, s), _RGB(r, g, b), BF
    _LIMIT 1000
LOOP


FUNCTION Rand (num)
    DIM z AS _INTEGER64, k AS _INTEGER64
    seed2 = seed * TIMER
    k = seed / 53668
    seed = 40014 * (seed - k * 53668) - k * 12211
    IF seed < 0 THEN seed = seed + 2147483563
    k = seed2 / 52774
    seed2 = 40692 * (seed2 - k * 52774) - k * 3791
    IF seed2 < 0 THEN seed2 = seed2 + 2147483563
    z = seed - seed2
    IF z < 1 THEN z = z + 2147483563
    Rand = z * 4.656613E-10 * 190000000 MOD num
END FUNCTION

Find my programs here in Dav's QB64 Corner
Reply
#2
Hmm, this works in older QB64 but I just tried it in QB84-PE and it's not working.  I should of tested it in that before posting .  Hang on and let me see if I can get it working in QB64-PE.

EDIT: Now it's working, had to add a _DELAY .25 at the beginning so _SCREENX/_SCREENY would be there.

- Dav

Find my programs here in Dav's QB64 Corner
Reply
#3
(09-30-2023, 11:06 PM)Dav Wrote: Hmm, this works in older QB64 but I just tried it in QB84-PE and it's not working.  I should of tested it in that before posting .  Hang on and let me see if I can get it working in QB64-PE.

EDIT: Now it's working, had to add a _DELAY .25 at the beginning so _SCREENX/_SCREENY would be there.

- Dav

Your seed sucks (dont take it personal), just set it to Timer.

No delay needed.
b = b + ...
Reply
#4
(09-30-2023, 11:19 PM)bplus Wrote:
(09-30-2023, 11:06 PM)Dav Wrote: Hmm, this works in older QB64 but I just tried it in QB84-PE and it's not working.  I should of tested it in that before posting .  Hang on and let me see if I can get it working in QB64-PE.

EDIT: Now it's working, had to add a _DELAY .25 at the beginning so _SCREENX/_SCREENY would be there.

- Dav

Your seed sucks (dont take it personal), just set it to Timer.

No delay needed.

Gosh - I could had sworn when I tried it that way I was getting patterns (I tested with PSET).  But, it does seems to be working with just timer.

EDIT: Oh I bet that was when I was putting seed=timer inside the function at first, which didn't work well.

- Dav

Find my programs here in Dav's QB64 Corner
Reply
#5
(09-30-2023, 11:27 PM)Dav Wrote:
(09-30-2023, 11:19 PM)bplus Wrote:
(09-30-2023, 11:06 PM)Dav Wrote: Hmm, this works in older QB64 but I just tried it in QB84-PE and it's not working.  I should of tested it in that before posting .  Hang on and let me see if I can get it working in QB64-PE.

EDIT: Now it's working, had to add a _DELAY .25 at the beginning so _SCREENX/_SCREENY would be there.

- Dav

Your seed sucks (dont take it personal), just set it to Timer.

No delay needed.

Gosh - I could had sworn when I tried it that way I was getting patterns (I tested with PSET).  But, it does seems to be working with just timer.

EDIT: Oh I bet that was when I was putting seed=timer inside the function at first, which didn't work well.

- Dav

Or maybe it has to be past a certain time for timer to work? I just got lucky (i'm due).
b = b + ...
Reply
#6
http://qb64phoenix.com/forum/showthread.php?tid=173 -- Might be interested in this old read as well.
Reply
#7
I actually gave this some serious thought about 15 years ago and was going to build a proof of concept around an Arduino build.

Tune a receiver to 1.42 MHz (the hydrogen band) and sample the incoming signal 64 times. Above a certain noise threshold becomes a 1, at or below becomes a 0, then normalize the result to between 0 and .9999 repeating.

I envisioned Wifi chips could have the included 1.42 MHz receiver or a simple receiver circuit built into every motherboard for software to access to get true random numbers.

Never built it. Maybe I should dust this project off.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#8
(10-01-2023, 12:12 AM)SMcNeill Wrote: http://qb64phoenix.com/forum/showthread.php?tid=173 -- Might be interested in this old read as well.

Interesting.  Thanks for the link.

- Dav

Find my programs here in Dav's QB64 Corner
Reply
#9
I would just set seed = timer
and run

Code: (Select All)
Do
    r = Rand(100)
    Print r;
    x = x + 1
    If x = 10 Then Exit Do
Loop
which seems to be quite accurate.
Reply
#10
randomize timer ???
Why not yes ?
Reply




Users browsing this thread: 1 Guest(s)