Long stairs: Difference between revisions

Content added Content deleted
(Added 11l)
Line 287: Line 287:
190 PRINT TIMET/10000
190 PRINT TIMET/10000
200 PRINT STEPST/10000</lang>
200 PRINT STEPST/10000</lang>

=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''

Neither the C nor the Go-based implmentations of jq currently have a
built-in PRNG, so for the present task
/dev/urandom is used as a source of entropy by invoking jq as follows:

<pre>
cat /dev/urandom | tr -cd '0-9' | fold -w 1 | jq -nrf long-stairs.jq
</pre>
where long-stairs.jq contains the jq program shown below.
For simplicity and since the intermediate output has the character of
a probe, the program uses `debug` statements to show the intermediate results.
<lang jq># 0 <= output < $n
def rand($n):

def ntimes(f): range(0; .) | f;
# input: an array of length ($n|tostring|length)
def r:
. as $in
| (join("") | tonumber) as $m
| if $m < $n then $m
else $in[1:] + [input] | r
end;
[$n | tostring | length | ntimes(input)] | r;

# $n specifies the number of iterations
def task($n):
(reduce range(1; $n + 1) as $trial (null;
.sbeh = 0
| .slen = 100
| .secs = 0
| until (.sbeh >= .slen;
.sbeh += 1
| reduce range(1;6) as $wiz (.;
if (rand( .slen) < .sbeh) then .sbeh += 1 else . end
| .slen += 1 )
| .secs += 1
| if ($trial == 1 and .secs > 599 and .secs < 610)
then ([.secs, .sbeh, .slen - .sbeh] | debug) as $debug | .
else .
end )
| .totalSecs += .secs
| .totalSteps += .slen ) ) ;

"Seconds steps behind steps ahead",
"------- ------------ -----------",
(1E4 as $n
| task($n)
| "\nAverage secs taken over \($n) trials: \(.totalSecs/$n)",
"Average final length of staircase: \(.totalSteps/$n)"
)</lang>
{{out}}
<pre>
Seconds steps behind steps ahead
------- ------------ -----------
["DEBUG:",[600,1916,1184]]
["DEBUG:",[601,1919,1186]]
["DEBUG:",[602,1920,1190]]
["DEBUG:",[603,1924,1191]]
["DEBUG:",[604,1926,1194]]
["DEBUG:",[605,1931,1194]]
["DEBUG:",[606,1934,1196]]
["DEBUG:",[607,1938,1197]]
["DEBUG:",[608,1942,1198]]
["DEBUG:",[609,1945,1200]]

Average secs taken over 10000 trials: 3211.962
Average final length of staircase: 16159.81
</pre>



=={{header|Julia}}==
=={{header|Julia}}==