Modified random distribution: Difference between revisions

Add Factor
(Added Go)
(Add Factor)
Line 23:
 
Show your output here, on this page.
 
=={{header|Factor}}==
<lang factor>USING: assocs assocs.extras formatting io kernel math
math.functions math.statistics random sequences
tools.memory.private ;
 
: modifier ( x -- y ) 0.5 over 0.5 < [ swap ] when - dup + ;
 
: random-unit-by ( quot: ( x -- y ) -- z )
random-unit dup pick call random-unit 2dup >
[ 2drop nip ] [ 3drop random-unit-by ] if ; inline recursive
 
: data ( n quot bins -- histogram )
'[ _ random-unit-by _ * >integer ] replicate histogram ;
inline
 
:: .histogram ( h -- )
h assoc-size :> buckets ! number of buckets
h sum-values :> total ! items in histogram
h values supremum :> max ! largest bucket (as in most occurrences)
40 :> size ! max size of a bar
 
total commas buckets
"Bin Histogram (%s items, %d buckets)\n" printf
 
h [| k v |
k buckets / dup buckets recip + "[%.2f, %.2f) " printf
size v * max / ceiling
[ "▇" write ] times bl bl v commas print
] assoc-each ;
 
"Modified random distribution of values in [0, 1):" print nl
100,000 [ modifier ] 20 data .histogram</lang>
{{out}}
<pre>
Modified random distribution of values in [0, 1):
 
Bin Histogram (100,000 items, 20 buckets)
[0.00, 0.05) ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 9,416
[0.05, 0.10) ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 8,498
[0.10, 0.15) ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 7,432
[0.15, 0.20) ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 6,415
[0.20, 0.25) ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 5,558
[0.25, 0.30) ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 4,489
[0.30, 0.35) ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 3,538
[0.35, 0.40) ▇▇▇▇▇▇▇▇▇▇▇ 2,532
[0.40, 0.45) ▇▇▇▇▇▇▇ 1,553
[0.45, 0.50) ▇▇▇ 490
[0.50, 0.55) ▇▇▇ 517
[0.55, 0.60) ▇▇▇▇▇▇▇ 1,467
[0.60, 0.65) ▇▇▇▇▇▇▇▇▇▇▇ 2,519
[0.65, 0.70) ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 3,559
[0.70, 0.75) ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 4,546
[0.75, 0.80) ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 5,569
[0.80, 0.85) ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 6,444
[0.85, 0.90) ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 7,428
[0.90, 0.95) ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 8,487
[0.95, 1.00) ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 9,543
</pre>
 
=={{header|Go}}==
1,808

edits