One of n lines in a file: Difference between revisions

no edit summary
(No longer draft as people seem able to follow the requirements OK.)
No edit summary
Line 113:
let rec aux i r =
if i >= n then r else
if (Random.float 1.0) < (1.0 /. floatint (i + 1)) = 0
then aux (succ i) i
else aux (succ i) r
Line 121:
let test ~n ~trials =
let ar = Array.make n 0 in
for i = 01 to pred trials do
let d = one_of_n n in
ar.(d) <- succ ar.(d)
Line 159:
 
=={{header|Python}}==
<lang python>from random import random as rndrandrange
 
def one_of_n(n):
Line 165:
choice = 0
for i in range(1, n):
if rnd() < 1. / randrange(i + 1.) == 0:
choice = i
return choice
Anonymous user