Author Topic: Simple PRNG  (Read 10247 times)

0 Members and 1 Guest are viewing this topic.

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Simple PRNG
« 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.
+[--->++<]>+.+++[->++++<]>.[--->+<]>+.-[---->+<]>++.+[->+++<]>+.+++++++++++.----------.[--->+<]>----.+[---->+<]>+++.---[->++++<]>.------------.+.++++++++++.+[---->+<]>+++.+[->+++<]>++.[--->+<]>+.[->+++<]>+.++++++++++.+.>++++++++++.

FellippeHeitor

  • Guest
Re: Simple PRNG
« Reply #1 on: December 22, 2018, 05:59:09 pm »
PRINT RND

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Re: Simple PRNG
« Reply #2 on: December 22, 2018, 06:15:43 pm »
Ok FellippeHeitor. Think you're funny don't you.
Make your own function. ;)
+[--->++<]>+.+++[->++++<]>.[--->+<]>+.-[---->+<]>++.+[->+++<]>+.+++++++++++.----------.[--->+<]>----.+[---->+<]>+++.---[->++++<]>.------------.+.++++++++++.+[---->+<]>+++.+[->+++<]>++.[--->+<]>+.[->+++<]>+.++++++++++.+.>++++++++++.

FellippeHeitor

  • Guest
Re: Simple PRNG
« Reply #3 on: December 22, 2018, 06:58:04 pm »
:-)

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Simple PRNG
« Reply #4 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?
« Last Edit: December 22, 2018, 07:46:57 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Simple PRNG
« Reply #5 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.)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Simple PRNG
« Reply #6 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.
You're not done when it works, you're done when it's right.

Offline SMcNeill

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

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Simple PRNG
« Reply #8 on: December 22, 2018, 10:35:13 pm »
sigh.

So do you use SLEEP to avoid closing a DO loop in all your programs?
You're not done when it works, you're done when it's right.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Simple PRNG
« Reply #9 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?
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Simple PRNG
« Reply #10 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:)
You're not done when it works, you're done when it's right.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Simple PRNG
« Reply #11 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.  ;)

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Simple PRNG
« Reply #12 on: December 23, 2018, 12:55:28 am »
Ah fuckit nevermind
Untitled.png
* Untitled.png (Filesize: 22.48 KB, Dimensions: 1366x724, Views: 395)
You're not done when it works, you're done when it's right.

Offline SMcNeill

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

FellippeHeitor

  • Guest
Re: Simple PRNG
« Reply #14 on: December 23, 2018, 02:24:21 am »
With GET #1, , a$(1) you probably wanted GET #1, , a$1