One of n lines in a file: Difference between revisions

Added 11l
(One of n lines in a file en Yabasic)
(Added 11l)
Line 24:
Note: This is a specific version of a Reservoir Sampling algorithm: https://en.wikipedia.org/wiki/Reservoir_sampling
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>F one_of_n(lines)
V choice = 0
L(line) lines
I random:(0..L.index) == 0
choice = line
R choice
 
F one_of_n_test(n = 10, trials = 1000000)
V bins = [0] * n
I n
L(i) 1..trials
bins[one_of_n(0 .< n)]++
R bins
 
print(one_of_n_test())</lang>
 
;Sample output:
<pre>
[100032, 100304, 99964, 100101, 100666, 100276, 99470, 99547, 99738, 99902]
</pre>
 
=={{header|Ada}}==
1,480

edits