Modified random distribution: Difference between revisions
Content added Content deleted
m (→{{header|UNIX Shell}}: change loop style) |
|||
Line 868: | Line 868: | ||
####### |
####### |
||
######## |
######## |
||
</pre> |
|||
=={{header|jq}}== |
|||
'''Adapted from [[#Wren|Wren]]''' |
|||
'''Works with jq, the C implementation of jq''' |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
Since jq does not currently have a built-in PRNG, |
|||
/dev/random will be used as a source of entropy. An invocation |
|||
of jq along the lines of the following would be appropriate |
|||
in a typical command-line environment: |
|||
<pre> |
|||
< /dev/random tr -cd '0-9' | fold -w 1 | jq -cnr -f modified.jq |
|||
</pre> |
|||
where "modified.jq" is the name of a file containing the following jq program. |
|||
<syntaxhighlight lang="jq"> |
|||
# Output: a PRN (integer) in range(0; .) |
|||
def prn: |
|||
if . == 1 then 0 |
|||
else . as $n |
|||
| (($n-1)|tostring|length) as $w |
|||
| [limit($w; inputs)] | join("") | tonumber |
|||
| if . < $n then . else ($n | prn) end |
|||
end; |
|||
def rgen: |
|||
1000 | prn / 1000; |
|||
# Modified random number generator. |
|||
# `modifier` should be a zero-arity filter |
|||
def rng(modifier): |
|||
{} |
|||
| until(.r1 and (.r2 < (.r1|modifier)); |
|||
.r1 = rgen |
|||
| .r2 = rgen ) |
|||
| .r1; |
|||
def modifier: |
|||
if (. < 0.5) then 2 * (0.5 - .) |
|||
else 2 * (. - 0.5) |
|||
end; |
|||
def N:100000; |
|||
def NUM_BINS: 20; |
|||
def HIST_CHAR: "■"; |
|||
def HIST_CHAR_SIZE: 500; |
|||
def binSize:1 / NUM_BINS; |
|||
def task: |
|||
# tidy decimals |
|||
def round($ndec): pow(10;$ndec) as $p | . * $p | round / $p; |
|||
def zpad($len): tostring | ($len - length) as $l | . + ("0" * $l); |
|||
def r: if . == 0 then "0.00" else round(2) | zpad(4) end; |
|||
reduce range(0; N) as $i ([]; |
|||
rng(modifier) as $rn |
|||
| (($rn / binSize)|floor) as $bn |
|||
| .[$bn] += 1) |
|||
| {bins: .} |
|||
| "Modified random distribution with \(N) samples in range [0, 1):", |
|||
" Range Number of samples within that range", |
|||
(foreach range(0; NUM_BINS) as $i (.; |
|||
(HIST_CHAR * (((.bins[$i] // 0) / HIST_CHAR_SIZE) | round)) as $hist |
|||
| .emit = "\(binSize * $i|r) - \($hist) \(.bins[$i] // 0)" ) |
|||
| .emit); |
|||
task |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
Modified random distribution with 100000 samples in range [0, 1): |
|||
Range Number of samples within that range |
|||
0.00 - ■■■■■■■■■■■■■■■■■■■ 9585 |
|||
0.05 - ■■■■■■■■■■■■■■■■■ 8489 |
|||
0.10 - ■■■■■■■■■■■■■■■ 7583 |
|||
0.15 - ■■■■■■■■■■■■■ 6291 |
|||
0.20 - ■■■■■■■■■■■ 5408 |
|||
0.25 - ■■■■■■■■■ 4633 |
|||
0.30 - ■■■■■■■ 3608 |
|||
0.35 - ■■■■■ 2433 |
|||
0.40 - ■■■ 1467 |
|||
0.45 - ■ 525 |
|||
0.50 - ■ 519 |
|||
0.55 - ■■■ 1480 |
|||
0.60 - ■■■■■ 2424 |
|||
0.65 - ■■■■■■■ 3670 |
|||
0.70 - ■■■■■■■■■ 4304 |
|||
0.75 - ■■■■■■■■■■■ 5463 |
|||
0.80 - ■■■■■■■■■■■■■ 6543 |
|||
0.85 - ■■■■■■■■■■■■■■■ 7455 |
|||
0.90 - ■■■■■■■■■■■■■■■■■■ 8759 |
|||
0.95 - ■■■■■■■■■■■■■■■■■■■ 9361 |
|||
</pre> |
</pre> |
||