Random integer 1..5, can be done a few other ways (not sure if your floor
creates bias).
5 random 1 +
5 [1,b] random
Not sure you need ``>integer`` conversion in dice7, should already be
integers.
Your ``roll`` word can be replaced with ``replicate``, is simpler, for
example:
10 [ dice7 ] replicate
Related to that, you can pass your word like ``\ dice7``, or you can pass a
quotation like ``[ dice7 ]``, sometimes the latter is easier and doesn't
require calling ``execute``.
Your ``count-diceX-outcomes`` might be simpler using math.statistics:
histogram sort-keys values
Your ``verify`` doesn't need to ``map vall?``, can just call ``all?``
(although I can see your version being helpful for debugging).
We've had some rosetta-code contributions added to the main repository, so
if you want to contribute any that would be great:
https://github.com/slavapestov/factor/tree/master/extra/rosetta-code
(This way we can maintain them if/as the language changes, and try and
contribute them back to the main wiki they run).
Best,
John.
On Thu, Apr 9, 2015 at 12:51 PM, Alexander Iljin <ajs...@yandex.ru> wrote:
> Hello again!
>
> I'm learning Factor little by little. Currently I'm trying to contribute
> a solution to Rosetta Code for the following pair of problems:
>
> http://rosettacode.org/wiki/Seven-sided_dice_from_five-sided_dice
> http://rosettacode.org/wiki/Simple_Random_Distribution_Checker
>
> The code that I've got so far can be seen below. Please, comment and
> criticize:
>
> USING: kernel random math math.functions math.vectors sequences locals
> prettyprint ;
> IN: dice7
>
> ! Output a random number 1..5.
> : dice5 ( -- x )
> random-unit 5 * floor 1 + >integer
> ;
>
> ! Output a random number 1..7 using dice5 as randomness source.
> : dice7 ( -- x )
> 0 [ dup 21 < ] [ drop dice5 5 * dice5 + 6 - ] do until
> 7 rem 1 + >integer
> ;
>
> ! Roll dice using the passed word the given number of times and produce an
> ! array with roll results.
> ! Sample call: \ dice7 1000 roll
> : roll ( word: ( -- x ) times -- array )
> iota [ drop dup execute( -- x ) ] map
> nip
> ;
>
> ! Input array contains outcomes of a number of die throws. Each die result
> is
> ! an integer in the range 1..X. Calculate and return the number of each
> ! of the results in the array so that in the first position of the result
> ! there is the number of ones in the input array, in the second position
> ! of the result there is the number of twos in the input array, etc.
> : count-diceX-outcomes ( array X -- array )
> iota [ 1 + dupd [ = ] curry count ] map
> swap length
> over sum
> assert=
> ;
>
> ! Verify distribution uniformity/Naive. Delta is the acceptable deviation
> ! from the ideal number of items in each bucket, expressed as a fraction of
> ! the total count. Sides is the number of die sides. Rnd-func is a word
> that
> ! produces a random number on stack in the range [1..sides], times is the
> ! number of times to call it.
> ! Sample call: 0.02 7 \ dice7 100000 verify
> :: verify ( delta sides rnd-func: ( -- random ) times -- )
> rnd-func times roll
> sides count-diceX-outcomes
> dup .
> times sides / :> ideal-count
> ideal-count v-n vabs
> times v/n
> delta [ < ] curry map
> vall? [ "Random enough" . ] [ "Not random enough" . ] if
> ;
>
>
> ! Call verify with 1, 10, 100, ... 1000000 rolls of 7-sided die.
> : verify-all ( -- )
> { 1 10 100 1000 10000 100000 1000000 }
> [| times | 0.02 7 \ dice7 times verify ] each
> ;
>
>
> ---=====---
> Александр
>
>
> ------------------------------------------------------------------------------
> BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
> Develop your own process in accordance with the BPMN 2 standard
> Learn Process modeling best practices with Bonita BPM through live
> exercises
> http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual-
> event?utm_
> source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF
> _______________________________________________
> Factor-talk mailing list
> Factor-talk@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
------------------------------------------------------------------------------
BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
Develop your own process in accordance with the BPMN 2 standard
Learn Process modeling best practices with Bonita BPM through live exercises
http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual- event?utm_
source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk