Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #134229

Re: Generating a random sequence of Forth words

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: Generating a random sequence of Forth words
Date 2025-10-01 17:10 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2025Oct1.191034@mips.complang.tuwien.ac.at> (permalink)
References <2025Sep30.183350@mips.complang.tuwien.ac.at> <mk4a3oFovluU1@mid.individual.net>

Show all headers | View raw


minforth <minforth@gmx.net> 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/

Back to comp.lang.forth | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Generating a random sequence of Forth words anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2025-09-30 16:33 +0000
  Re: Generating a random sequence of Forth words minforth <minforth@gmx.net> - 2025-10-01 11:20 +0200
    Re: Generating a random sequence of Forth words anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2025-10-01 17:10 +0000
  Re: Generating a random sequence of Forth words Hans Bezemer <the.beez.speaks@gmail.com> - 2025-10-01 17:11 +0200
    Re: Generating a random sequence of Forth words minforth <minforth@gmx.net> - 2025-10-01 20:42 +0200
      Re: Generating a random sequence of Forth words dxf <dxforth@gmail.com> - 2025-10-02 19:49 +1000
        Re: Generating a random sequence of Forth words albert@spenarnc.xs4all.nl - 2025-10-02 13:07 +0200
          Re: Generating a random sequence of Forth words dxf <dxforth@gmail.com> - 2025-10-03 18:22 +1000
        3dup again (was: Generating a random sequence of Forth words) anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2025-10-02 20:44 +0000
          Re: 3dup again (was: Generating a random sequence of Forth words) albert@spenarnc.xs4all.nl - 2025-10-03 11:02 +0200
            Re: 3dup again minforth <minforth@gmx.net> - 2025-10-03 11:09 +0200
              Re: 3dup again anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2025-10-04 08:04 +0000
          Re: 3dup again Hans Bezemer <the.beez.speaks@gmail.com> - 2025-10-05 11:29 +0200
  Re: Generating a random sequence of Forth words antispam@fricas.org (Waldek Hebisch) - 2025-10-15 19:19 +0000
    Re: Generating a random sequence of Forth words anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2025-10-24 15:55 +0000

csiph-web