Constrained random points on a circle: Difference between revisions

Content added Content deleted
(add lambdatalk)
(Updated to work with version 1.4 of Nim. Used standard library "sample" to get a random element from a sequence. Doubled horizontal scale.)
Line 2,134: Line 2,134:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Python}}
{{trans|Python}}
<lang nim>import tables, math, strutils, complex, random
<lang nim>import tables, math, complex, random

proc random[T](a: openarray[T]): T =
result = a[rand(low(a)..len(a))]


type Point = tuple[x, y: int]
type Point = tuple[x, y: int]
Line 2,146: Line 2,143:
for x in -15..15:
for x in -15..15:
for y in -15..15:
for y in -15..15:
if abs((x.float, y.float)) in 10.0..15.0:
if abs(complex(x.float, y.float)) in 10.0..15.0:
possiblePoints.add((x,y))
possiblePoints.add((x,y))


randomize()
randomize()
for i in 0..100: world.inc possiblePoints.random
for i in 0..100: world.inc possiblePoints.sample


for x in -15..15:
for x in -15..15:
Line 2,156: Line 2,153:
let key = (x, y)
let key = (x, y)
if key in world and world[key] > 0:
if key in world and world[key] > 0:
stdout.write min(9, world[key])
stdout.write ' ' & $min(9, world[key])
else:
else:
stdout.write ' '
stdout.write " "
echo ""</lang>
echo ""</lang>

Output:
{{out}}
<pre> 1
1211 1
<pre> 1
1 1 1
1 1 3 1 1
3
3 11
1 1 1 1
1 11 1 1 1
1 2 1 1 1 1 1
122 21 1
2
1 1 1 1 1 1
1 1 2
1
1
1 1 1 1
1 1
1 2 1
11 11
1 1
1 1 1
1 1
1
1 1 1
11 1
1 1
1 1 11
1
2 1
1 1 1 1 1 1
1
1
1 1
1 1
1 1
2
11
1 1 2
11
1 1
1 1 111
1 1 1 1 1
1 1 1 1
1
1 11
1 2 2 1 1
1 1 1
1 1 1 1
12 2 11 1 1
1 1 1
1 1 1 1 1
1 1 1
11 1
1 1
1 </pre>
1 1 2 1 1 2
1 1 1 1
1 </pre>


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