RPG attributes generator: Difference between revisions

Line 2,281:
79 -> [15,15,11,17,12,9]
76 -> [14,12,9,15,15,11]</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq.'''
 
In this entry, /dev/random is used as a source of entropy,
via the invocation:
<pre>
< /dev/random tr -cd '0-9' | fold -w 1 | jq -Mcnr -f rgp-attributes.jq
</pre>
where rgp-attributes.jq is a file containing the jq program shown below.
 
'''The jq program'''
<syntaxhighlight lang=jq>
def count(s): reduce s as $x (0; .+1);
 
# Output: a PRN in range(0;$n) where $n is .
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;
 
# Output: [$four, $sum]
# where $four is an array of 4 pseudo-random integers between 1 and 6 inclusive,
# and $sum records the sum of the 3 largest values.
def generate_RPG:
[range(0; 4) | (1 + (7|prn) )] as $four
| [$four, ($four|sort|.[-3:]|add)];
 
# Input: $six as produced by [range(0;6) | generate_RPG]
# Determine if the following conditions are met:
# - the total of all 6 of the values at .[-1] is at least 75;
# - at least 2 of these values must be 15 or more.
def ok:
([.[][-1]] | add) as $sum
| $sum >= 75 and
count( (.[][1] >= 15) // empty) >= 2;
 
# First show [range(0;6) | generate_RPG]
# and then determine if it meets the "ok" condition;
# if not, repeat until a solution has been found.
def task:
[range(0;6) | generate_RPG] as $six
| ([$six[][-1]] | add) as $sum
| $six[], "Sum: \($sum)",
if $six | ok then "All done."
else "continuing searching ...",
({}
| until(.emit;
[range(0;6) | generate_RPG] as $six
| ([$six[][-1]] | add) as $sum
| if $six | ok
then .emit = {$six, $sum}
else .
end).emit
| (.six[], "Sum: \(.sum)" ) )
end;
 
task
</syntaxhighlight>
{{Output}}
''Example of a run which only one round of throwing the four dice''
<pre>
[3,6,7,5]
[3,2,1,1]
[7,6,5,3]
[3,4,7,5]
[1,3,6,1]
[6,5,3,2]
Sum: 82
All done.
</pre>
''Example of a run requiring more than one round of throwing the four dice''
<pre>
[[6,6,1,3],15]
[[3,3,6,5],14]
[[7,2,7,5],19]
[[6,6,5,5],17]
[[5,7,3,4],16]
[[6,1,2,5],13]
Sum: 94
continuing searching ...
[[7,7,2,7],21]
[[7,4,1,6],17]
[[7,3,3,1],13]
[[7,7,6,4],20]
[[2,3,6,5],14]
[[6,1,5,2],13]]
Sum: 98
</pre>
 
=={{header|Julia}}==
2,442

edits