6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri May 17, 2024 10:40 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: How does this work?
PostPosted: Tue Oct 30, 2018 7:56 pm 
Offline

Joined: Wed Jul 18, 2018 12:12 pm
Posts: 96
I was looking again at the simple random number generator shown in 'Thinking Forth' and find myself wondering how the 'choose' word works. The code is below with my comments. The following line is what confuses me:

Code:
um* swap drop ( 0 u within )


You pass the range of random number on the stack, it calls the random function so you have ( range random -- ). They are multiplied and pushed to stack as a double. The swap puts the low word/cell on top of the stack and then it is dropped. How does this make the number returned within range specified? It might be simple and I am overlooking it but it has me stumped at this point.

Code:
\ File: Random
\ From Thinking FOrth by Leo Brodie

\ puts next dictionary address in rnd as seed
variable rnd here rnd !

: random ( -- u )
   rnd @ 31421 * 6927 + dup rnd !
;

\ multiplies maxval, random number
\ pushes double length number to stack
\ swaps high cell/low cell, drops low?
: choose ( u maxval -- u rand )
   random \ get random number
   um* swap drop ( 0 u within )
;

:test
    10 choose .
;


Top
 Profile  
Reply with quote  
 Post subject: Re: How does this work?
PostPosted: Wed Oct 31, 2018 5:42 am 
Offline

Joined: Sun Jul 28, 2013 12:59 am
Posts: 235
This... is actually kindof neat. And it's simple fixed-point math.

The word RANDOM returns a "random" cell-sized value (it's not particularly random, and the seed is atrocious). Think of this as being a value between 0 and just below 1. UM* multiplies that value by an exclusive upper bound, returning a 2-cell fixed-point result, the integer part being the high bits of the result and the fractional part being the low bits. It then discards the fractional part.


Top
 Profile  
Reply with quote  
 Post subject: Re: How does this work?
PostPosted: Wed Oct 31, 2018 1:05 pm 
Offline

Joined: Wed Jul 18, 2018 12:12 pm
Posts: 96
nyef wrote:
This... is actually kindof neat. And it's simple fixed-point math.

The word RANDOM returns a "random" cell-sized value (it's not particularly random, and the seed is atrocious). Think of this as being a value between 0 and just below 1. UM* multiplies that value by an exclusive upper bound, returning a 2-cell fixed-point result, the integer part being the high bits of the result and the fractional part being the low bits. It then discards the fractional part.


Thanks! I also asked on the Forth Facebook group and it sort of dovetailed with a discussion a few days ago about the use of um* vs. mod. It turns out to be better to use um*.

The seed might not be excellent but it works on every system, which is why I'm guessing why it was used. It is not the most random of random generators but it is short, simple and fairly good.

I'm still at the point of 'translating' Forth to a more familiar language in my head, sort of like you would do when learning a new spoken language. I think this hinders understanding how tings work at times.


Top
 Profile  
Reply with quote  
 Post subject: Re: How does this work?
PostPosted: Wed Oct 31, 2018 2:41 pm 
Offline

Joined: Mon Jan 07, 2013 2:42 pm
Posts: 576
Location: Just outside Berlin, Germany
Yeah, I tried to mentally convert Forth to Python at the beginning, and mostly I got a headache. It's just too different. Now I have the reverse problem: I expect computer programs to start at the beginning, go to end, and then stop, and not to have some definition of some function stored in whatever other file somewhere. Also, I tend to want to use DEFER constructs for everything. Given this is basically a way of using first-order functions, I may have to seriously look at Haskell now :D .


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: