Posts: 713
Threads: 117
Joined: Apr 2022
Reputation:
105
09-30-2023, 11:01 PM
(This post was last modified: 09-30-2023, 11:10 PM by Dav.)
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
Posts: 713
Threads: 117
Joined: Apr 2022
Reputation:
105
09-30-2023, 11:06 PM
(This post was last modified: 09-30-2023, 11:12 PM by Dav.)
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
Posts: 3,885
Threads: 174
Joined: Apr 2022
Reputation:
202
09-30-2023, 11:19 PM
(This post was last modified: 09-30-2023, 11:20 PM by bplus.)
(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 + ...
Posts: 713
Threads: 117
Joined: Apr 2022
Reputation:
105
09-30-2023, 11:27 PM
(This post was last modified: 09-30-2023, 11:32 PM by Dav.)
(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
Posts: 3,885
Threads: 174
Joined: Apr 2022
Reputation:
202
(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 + ...
Posts: 2,673
Threads: 325
Joined: Apr 2022
Reputation:
214
Posts: 1,277
Threads: 120
Joined: Apr 2022
Reputation:
100
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
Posts: 713
Threads: 117
Joined: Apr 2022
Reputation:
105
(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
Posts: 369
Threads: 71
Joined: Jul 2022
Reputation:
14
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.
Posts: 55
Threads: 4
Joined: Apr 2022
Reputation:
5
randomize timer ???
Why not yes ?
|