QB64.org Forum

Active Forums => Programs => Topic started by: Zeppelin on December 22, 2018, 05:53:19 pm

Title: Simple PRNG
Post 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.

Code: QB64: [Select]
  1. seed = TIMER
  2.     nSeed = VAL(MID$(STR$(seed ^ 2), 1, 4))
  3.     PRINT nSeed
  4.     seed = nSeed
  5.     _DELAY (0.1)
  6.  

Yep thats it. 7 lines. (you dont even need the loop)

Zep.
Title: Re: Simple PRNG
Post by: FellippeHeitor on December 22, 2018, 05:59:09 pm
PRINT RND
Title: Re: Simple PRNG
Post by: Zeppelin on December 22, 2018, 06:15:43 pm
Ok FellippeHeitor. Think you're funny don't you.
Make your own function. ;)
Title: Re: Simple PRNG
Post by: FellippeHeitor on December 22, 2018, 06:58:04 pm
:-)
Title: Re: Simple PRNG
Post by: STxAxTIC on December 22, 2018, 07:45:45 pm
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?
Title: Re: Simple PRNG
Post by: SMcNeill on December 22, 2018, 08:13:04 pm
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.)
Title: Re: Simple PRNG
Post by: STxAxTIC on December 22, 2018, 10:02:18 pm
Code: QB64: [Select]
  1. OPEN “QB64.EXE” FOR BINARY AS #1
  2.   GET #1, , a$(1)
  3.   PRINT ASC(a$(1))/ 256

I see what you are trying to say but pound-for-pound that is the oddest program flow I can imagine.
Title: Re: Simple PRNG
Post by: SMcNeill on December 22, 2018, 10:11:59 pm
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.   ;)
Title: Re: Simple PRNG
Post by: STxAxTIC on December 22, 2018, 10:35:13 pm
sigh.

So do you use SLEEP to avoid closing a DO loop in all your programs?
Title: Re: Simple PRNG
Post by: SMcNeill on December 22, 2018, 10:54:38 pm
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?
Title: Re: Simple PRNG
Post by: STxAxTIC on December 22, 2018, 11:27:36 pm
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:)
Title: Re: Simple PRNG
Post by: SMcNeill on December 22, 2018, 11:56:24 pm
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.  ;)

Title: Re: Simple PRNG
Post by: STxAxTIC on December 23, 2018, 12:55:28 am
Ah fuckit nevermind
Title: Re: Simple PRNG
Post by: SMcNeill on December 23, 2018, 01:14:54 am
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.
Title: Re: Simple PRNG
Post by: FellippeHeitor on December 23, 2018, 02:24:21 am
With GET #1, , a$(1) you probably wanted GET #1, , a$1
Title: Re: Simple PRNG
Post by: SMcNeill on December 23, 2018, 02:36:26 am
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.
Title: Re: Simple PRNG
Post by: Cobalt on December 23, 2018, 08:32:02 pm
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.
Title: Re: Simple PRNG
Post by: TempodiBasic on December 25, 2018, 07:37:02 pm
Code: QB64: [Select]

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?