Constrained random points on a circle
You are encouraged to solve this task according to the task description, using any language you may know.
The task is to generate a stream of 100 uniformly distributed random points (x,y integer pairs) that lie in a circular disc at 10 to 15 units from its center; and then display/plot them to show a fuzzy circle.
There are several possible approaches, depending on your language. One is simply to generate random pairs of integers and filter out those that don't satisfy the equation
10 <= sqrt( x*x + y*y ) <= 15
Another is to precalculate the set of all possible points (there are 404 of them) and select from this set. Yet another is to use real-valued polar coordinates then snap to integer Cartesian coordinates. I'm sure there are others.
SystemVerilog
<lang SystemVerilog>program main;
bit [39:0] bitmap [40];
class Point; rand bit signed [4:0] x; rand bit signed [4:0] y;
constraint on_circle_edge { (10*10) <= (x*x + y*y); (x*x + y*y) <= (15*15); };
function void do_point(); randomize; bitmap[x+20][y+20] = 1; endfunction endclass
initial begin Point p = new; repeat (100) p.do_point; foreach (bitmap[row]) $display( "%b", bitmap[row]); end
endprogram</lang>
Piping the output through sed to improve the contrast of the output:
% vcs -sverilog -R circle.sv | sed 's/0/ /g' 1 11 1 1 1 1 1 11 1 1 1 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 11 1 11 1 1 1 1 1 1 1 1 1 1 1 1 11 1 1 11 1111 1 1 111 1 11 1 111 1 11 1 1 1 1 1 1 1 11 1 1 1 11 1 1
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:
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