QB64.org Forum
Active Forums => Programs => Topic started by: Zeppelin on December 22, 2018, 05:53:19 pm
-
Hey everyone,
Today I bring you probably the most simple PRNG (pseudorandom number generator). If anyone would like to out do me via less lines or (somehow) making the program simpler, go ahead.
Yep thats it. 7 lines. (you dont even need the loop)
Zep.
-
PRINT RND
-
Ok FellippeHeitor. Think you're funny don't you.
Make your own function. ;)
-
:-)
-
PRINT RND doesn't give randomness.
10 PRINT RND
20 END
^^^ Prints the same exact number on each run, which is by definition not random, and is exactly why you need some kind of a seed. This begs the much worse question "how does the computer make randomness from a seed", which sends us right back to the top of this thread. I'd say keep working on it.
... But make sure the thing you engineer gives true randomness. There are whole branches of computation devoted to this question.
Unrelated but interesting:
Are the digits of pi effectively random? If I want 1000 reliably random numbers can I go to the trillionth digit of pi and just start reading the number of digits I need? What if *everyone* got their random numbers this way?
-
How about this style routine:
OPEN “QB64.EXE” FOR BINARY AS #1
DO
GET #1, , a$(1)
PRINT ASC(a$(1))/ 256
SLEEP
CLOSE
Use any file as the pseudo-random generator, but probability values may be skewed without an even distributation. (Think a text file with letter E occurrence vs letter Z.)
-
I see what you are trying to say but pound-for-pound that is the oddest program flow I can imagine.
-
It’s not so odd. Get a single byte off a file, use it for the random number. Get bytes sequentially so they don’t repeat as often. ;)
-
sigh.
So do you use SLEEP to avoid closing a DO loop in all your programs?
-
sigh.
So do you use SLEEP to avoid closing a DO loop in all your programs?
Only for illustration purposes. Who can read the screen scroll otherwise?
-
Something tells me you still don't realize there's no closing LOOP on your DO.
;-)
(here comes the part where you explain having known all along:)
-
Something tells me you still don't realize there's no closing LOOP on your DO.
;-)
(here comes the part where you explain having known all along:)
No, here’s where the part that I explain I left it off on purpose so you can paste it in the IDE and have a learning experience. :P
As I’ve said before, unless it’s in a code box, it’s just pseudo-code to illustrate a single purpose — in this case, using a file to create a random number. My lesson on DO...LOOP was covered last week, but you missed that class. ;)
-
Ah fuckit nevermind
-
Aye. There’s no LOOP after the DO. There’s also no dice generator, game, or anything else after the CLOSE...
I really didn’t think it was necessary to type anything more, as it’s a PITA to write code on the iPad. I *only* posted what was necessary to get a random number... Inside a DO — LOOP, as it is, would fail to run properly anyway — there’s a CLOSE statement in there which would invalidate the next run of GET.
I left it incomplete, only to show the RND process. What occurs after that would, of course, be up to whoever is using it.
As I’ve said before: If it’s not in a code box, it’s not complete code; it’s pseudo-code, at best, just to illuminate a specific idea.
-
With GET #1, , a$(1) you probably wanted GET #1, , a$1
-
With GET #1, , a$(1) you probably wanted GET #1, , a$1
Aye. I don’t use variables in such a manner usually, (normally I DIM my strings to avoid typing the $ as much as possible), so I gaffed the syntax. a$(1) is a string array; a$1 is a single byte character. a$1 would work much better in this case.
-
Could probably just set up a small radio telescope and use background noise from space, but then I wouldn't know where to get a radio telescope. Don't think you'll find a much better source for random values than that. I think Random.org(or is that .com) uses atmospheric noise.
-
just timer changes itself and gives a seed.
@Cobalt
I don't know _RADIOTELESCOPE's synthax....
sorry for my bad knowledge! :-)
Moreover is it more predictable the flow of the sea on the beach?