Constrained random points on a circle: Difference between revisions

→‎Tcl: Added implementation
(revise task description to not bias the solution towards particular implementation)
(→‎Tcl: Added implementation)
Line 74:
 
</pre>
 
=={{header|Tcl}}==
<lang tcl>package require Tcl 8.5
 
# Make somewhere to store the counters
set ary [lrepeat 31 [lrepeat 31 0]]
 
# Generate 100 random points at specified distance from the centre
for {set i 0} {$i < 100} {incr i} {
while 1 {
set x [expr {int(31*rand())}]
set y [expr {int(31*rand())}]
set d2 [expr {($x-15)**2 + ($y-15)**2}]
if {$d2 >= 10**2 && $d2 <= 15**2} break
}
 
# Increment the counter for that location
lset ary $y $x [expr {1 + [lindex $ary $y $x]}]
}
 
# Simple renderer
foreach line $ary {
foreach c $line {
puts -nonewline [expr {$c == 0 ? " " : $c > 9 ? "X" : $c}]
}
puts ""
}</lang>
Example output:
<pre>
1
1 1
1 1 1 2 1 1
11 1 1 1
11 1 1 1
1 1
1 12 1
1 1 1
1 1 1
1 1
1 1 1
1 2
1
1 1
2 1 2
2 1
1
1 11
1 1
1
1 1
2 1 1
1 1
1 1 1 11 1
2 1 1 11
11 1 1 1
1 2 1 11
121 1 1
1 1 1
1
</pre>
Anonymous user