Jump to content

Constrained random points on a circle: Difference between revisions

no edit summary
No edit summary
Line 892:
* * ** *
*</pre>
=={{header|freebasic}}==
Pre calculate and plot 100 points to the console
<lang freebasic>
 
'Free Basic version .9
 
#define Intrange(f,l) int(Rnd*(((l)+1)-(f))+(f))
 
Type pair
As Integer x,y
End Type
 
Operator =(a As pair,b As pair) As Integer
Return a.x=b.x And a.y=b.y
End Operator
 
Function NotInArray(a() As pair,n As pair) As Integer
For z As Integer=Lbound(a) To Ubound(a)
If a(z)=n Then Return 0
Next z
Return -1
End Function
 
Redim As pair pts(0)
Dim As Integer x,y,counter
Do
counter=counter+1
x=IntRange(-20,20)
y=IntRange(-20,20)
var root=Sqr(x*x+y*y)
If 10<= root And root<=15 Then
If NotInArray(pts(),Type<pair>(x,y)) Then
Redim Preserve pts(1 To Ubound(pts)+1)
pts(Ubound(pts))=Type<pair>(x,y)
End If
End If
Loop Until counter=100000
 
'============== Plot to Console ======================
 
dim as integer yres=hiword(width)
dim as integer xres=loword(width)
 
#define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)
#define _X(num) int( map(0,xres,(num),0,loword(width)))
#define _Y(num) int( map(0,yres,(num),0,hiword(width)))
 
counter=0
For n As Integer=Lbound(pts) To Ubound(pts)
counter=counter+1
if counter <=100 then
var xpos=map(-20,20,pts(n).x,0,xres)
var ypos=map(-20,20,pts(n).y,0,yres)
locate _Y(ypos),_X(xpos)
print "*"
end if
Next n
 
print
locate 1,1
Print "Total number of points "; counter
print "Total number plotted ";100
print "done"
Sleep
</lang>
Console output:
<pre>
Total number of points 404
Total number plotted 100
done * *
* * *
* * * * * *
* * * * * * * * * *
* * * * *
* * * * * *
*
* * * * *
* * * * * *
* * * *
* * * * *
 
* * * * *
* * * * * *
* * *
* * * * * * * *
* * * *
* * * * * * * * * * *
* * * * *
 
 
</pre>
 
 
=={{header|Go}}==
Algorithm 1:
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.