One of n lines in a file: Difference between revisions

GP
m (Added Sidef)
(GP)
Line 1,250:
100620 99719 99928 99864 99760 100151 99553 100529 99800 100076
</pre>
 
=={{header|PARI/GP}}==
gp can't read individual lines from a file (PARI would be needed for that) but it can do the simulation easily. The <code>random()</code> function produces high-quality pseudorandom numbers (via Brent's [http://maths-people.anu.edu.au/~brent/pub/pub224.html XORGEN]) so the output passes a chi-square test easily (p = 0.848).
<lang parigp>one_of_n(n)={
my(chosen=1);
for(k=2,n,
if(random(k)==0, chosen=k)
);
chosen;
}
v=vector(10); for(i=1,1e6, v[one_of_n(10)]++); v</lang>
{{out}}
<pre>%1 = [99933, 100021, 100125, 100071, 99876, 99485, 100108, 100183, 99861, 100337]</lang>
 
=={{header|Pascal}}==