One of n lines in a file: Difference between revisions

Line 1,218:
Sample output:
<pre>[99832, 99958, 100281, 99601, 99568, 99689, 100118, 99753, 100659, 100541]</pre>
 
=={{header|jq}}==
{{trans|Wren}}
{{works with|jq}}
 
Since jq does not have a built-in PRNG, this entry assumes the
availability of an external source
of entropy such as /dev/random. The output shown below
is from a run of the following:
<lang sh>
export LC_ALL=C
< /dev/random tr -cd '0-9' | fold -w 4 | jq -Mnr -f program.jq
</lang>
 
'''program.jq'''
<lang jq># oneOfN presupposes an unbounded stream
# of 4-digit PRNs uniformly distributed on [0-9999]
def oneOfN:
reduce range(2; 1+.) as $i (1;
if (input / 10000) < (1/$i) then $i else . end);
def n: 10;
def repetitions: 1e6;
 
( reduce range(0; repetitions) as $i (null;
(n|oneOfN) as $num
| .[$num-1] += 1 )) as $freqs
| range(1; 1+n) | "Line\(.) \($freqs[.-1] )"</lang>
{{out}}
<pre>
Line1 99745
Line2 100351
Line3 99805
Line4 100240
Line5 100155
Line6 99525
Line7 100182
Line8 99738
Line9 100360
Line10 99899
</pre>
 
 
=={{header|Julia}}==
2,442

edits