Modified random distribution: Difference between revisions
Content added Content deleted
m (Added language identifier.) |
(→{{header|Ruby}}: Add Ruby) |
||
Line 1,138: | Line 1,138: | ||
0.90, 4268: ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ |
0.90, 4268: ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ |
||
0.95, 4758: ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ |
0.95, 4758: ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ |
||
</pre> |
|||
=={{header|Ruby}}== |
|||
<syntaxhighlight lang="ruby" line>def modifier(x) = (x - 0.5).abs * 2 |
|||
def mod_rand |
|||
loop do |
|||
random1, random2 = rand, rand |
|||
return random1 if random2 < modifier(random1) |
|||
end |
|||
end |
|||
bins = 15 |
|||
bin_size = 1.0/bins |
|||
h = {} |
|||
(0...bins).each{|b| h[b*bin_size] = 0} |
|||
tally = 50_000.times.map{ (mod_rand).div(bin_size) * bin_size}.tally(h) |
|||
m = tally.values.max/40 |
|||
tally.each {|k,v| puts "%f...%f %s %d" % [k, k+bin_size, "*"*(v/m) , v] } |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0.000000...0.066667 **************************************** 6241 |
|||
0.066667...0.133333 ********************************* 5286 |
|||
0.133333...0.200000 *************************** 4365 |
|||
0.200000...0.266667 ********************** 3576 |
|||
0.266667...0.333333 ***************** 2724 |
|||
0.333333...0.400000 *********** 1837 |
|||
0.400000...0.466667 ***** 925 |
|||
0.466667...0.533333 * 207 |
|||
0.533333...0.600000 ***** 843 |
|||
0.600000...0.666667 *********** 1761 |
|||
0.666667...0.733333 ***************** 2699 |
|||
0.733333...0.800000 *********************** 3629 |
|||
0.800000...0.866667 **************************** 4514 |
|||
0.866667...0.933333 ********************************* 5301 |
|||
0.933333...1.000000 *************************************** 6092 |
|||
</pre> |
</pre> |
||