Constrained random points on a circle: Difference between revisions

From Rosetta Code
Content added Content deleted
(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 + …')
 
(revise task description to not bias the solution towards particular implementation)
Line 1: Line 1:
{{task|Probability and statistics}}
{{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:
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
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.
and plot (display) them to show a fuzzy circle.


=={{header|SystemVerilog}}==
=={{header|SystemVerilog}}==

Revision as of 01:16, 3 September 2010

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 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