One of n lines in a file: Difference between revisions

Content added Content deleted
(Updated to work with Nim 1.4: replaced ".. <" with "..<"; replaced "import math" with "import random"; replaced "random(x+1)" with "rand(x)"; updated output.)
Line 1,445: Line 1,445:


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>import math
<lang nim>import random
randomize()
randomize()


proc oneOfN(n: int): int =
proc oneOfN(n: int): int =
result = 0
result = 0
for x in 0 .. <n:
for x in 0 ..< n:
if random(x+1) == 0:
if random(x) == 0:
result = x
result = x


Line 1,462: Line 1,462:
echo oneOfNTest()</lang>
echo oneOfNTest()</lang>
Output:
Output:
<pre>@[99912, 100048, 99894, 99697, 99806, 100316, 100209, 99898, 100027, 100193]</pre>
<pre>@[100454, 100110, 99882, 99733, 100171, 99724, 100111, 99874, 100159, 99782]</pre>


=={{header|OCaml}}==
=={{header|OCaml}}==