Constrained random points on a circle

From Rosetta Code
Revision as of 23:00, 2 September 2010 by rosettacode>Davewhipp (Created page with '{{task|Probability and statistics}} The task is to generate a set of 100 uniformly distributed random points (x,y integer pairs) that satisfy the constraint: 10 <= sqrt( x*x + …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
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 set of 100 uniformly distributed random points (x,y integer pairs) that satisfy the constraint:

10 <= sqrt( x*x + y*y ) <= 15

and plot (display) them to show a fuzzy circle.

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