Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.forth    |    Forth programmers eat a lot of Bratwurst    |    117,927 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 117,596 of 117,927    |
|    Anton Ertl to minforth    |
|    Re: Generating a random sequence of Fort    |
|    01 Oct 25 17:10:34    |
   
   From: anton@mips.complang.tuwien.ac.at   
      
   minforth writes:   
   >Am 30.09.2025 um 18:33 schrieb Anton Ertl:   
   >> Eventually I came up with the following approach: Starts with counters   
   >> for the different kinds of words/numbers. Whenever one word/number is   
   >> generated, its counter is reduced. At each step the probability of   
   >> generating a kind of word/number is proportional to the remaining   
   >> counter for that word/number. The same-depth requirement is   
   >> implemented by having the same initial counter for the number and   
   >> DROP. The no-underflow requirement is implemented by skipping the   
   >> output of a DROP if the count of remaining DROPs = the count of the   
   >> remaining numbers.   
   >>   
   >> Here's the code:   
   >>   
   >> create counts 3 cells allot   
   >>   
   >> : initcounts ( u -- )   
   >> dup 3 / dup counts ! dup counts 2 th ! 2* - counts cell+ ! ;   
   >>   
   >> : .counts ( -- )   
   >> counts 3 cell array>mem mem+do   
   >> i @ 4 .r   
   >> loop ;   
   >>   
   >> : countsth-- ( u -- )   
   >> -1 counts rot th +! ;   
   >>   
   >> : select1 ( u -- )   
   >> \ u is the index of the word   
   >> dup case   
   >> 0 of ." 9 " countsth-- endof   
   >> 1 of ." noop " countsth-- endof   
   >> 2 of counts @ counts 2 th @ u< if   
   >> ." drop " countsth--   
   >> else   
   >> drop endif   
   >> endof   
   >> endcase ;   
   >>   
   >> : sum ( addr u -- u2 )   
   >> \ addr u is an array with u elements, u2 is the sum of the elements   
   >> 0 -rot cell array>mem mem+do   
   >> i @ +   
   >> loop ;   
   >>   
   >> : genseq ( u -- )   
   >> initcounts   
   >> begin   
   >> counts 3 sum dup while   
   >> rnd um* nip ( sum )   
   >> 3 0 u+do ( sum1 )   
   >> counts i th @ 2dup u< if   
   >> i select1 then   
   >> - loop   
   >> drop   
   >> repeat   
   >> drop ;   
   ...   
   >How about   
   >1) quick generation of a suboptimal list   
   >2) create an array of pointers to the list elements   
   >3) random shuffle the array e.g.   
   >https://en.wikipedia.org/wiki/Fisher-Yates_shuffle   
      
   I thought about an approach based on this in between, with appropriate   
   changes to satisfy the requirements. Then I continued onwards to the   
   second approach shown above. The counters are a kind of compressed   
   form of "a suboptimal list". Every time you pick one of the elements   
   of the suboptimal list, the counter for that kind of element is   
   lowered by 1.   
      
   - anton   
   --   
   M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html   
   comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html   
    New standard: https://forth-standard.org/   
   EuroForth 2025 CFP: http://www.euroforth.org/ef25/cfp.html   
   EuroForth 2025 registration: https://euro.theforth.net/   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca