Anonymous user
Verify distribution uniformity/Naive: Difference between revisions
Verify distribution uniformity/Naive (view source)
Revision as of 17:07, 10 April 2015
, 9 years agoUpdate Factor implementation: some functions were simplified thanks to John Benediktsson.
(Add Factor implementation.) |
(Update Factor implementation: some functions were simplified thanks to John Benediktsson.) |
||
Line 459:
=={{header|Factor}}==
<lang factor>USING: kernel random
math math.functions math.statistics math.vectors math.ranges ;
IN: rosetta-code.dice7
! Output a random
: dice5 ( -- x )
5 [1,b] random
;
! Output a random
: dice7 ( -- x )
0 [ dup 21 < ] [ drop dice5 5 * dice5 + 6 - ] do until
7 rem 1 +
;
! Roll
! an array with roll results.
! Sample call:
: roll (
;
Line 486:
! 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-
histogram
swap [1,b] [ over [ 0 or ] change-at ] each
sort-keys values
;
! 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.
! 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
:: verify ( delta sides
sides
dup .
times sides / :> ideal-count
ideal-count v-n vabs
times v/n
delta [ < ] curry
;
Line 514:
: verify-all ( -- )
{ 1 10 100 1000 10000 100000 1000000 }
[| times | 0.02 7
;</lang>
Output:
<pre>USE: rosetta-code.dice7 verify-all
{ 0
"Not random enough"
{
"Not random enough"
{
"Not random enough"
{
"
{
"Random enough"
{
"Random enough"
{ 142599 141910 142524 143029 143353 142696 143889 }
"Random enough"</pre>
|