How to resolve the algorithm Constrained random points on a circle step by step in the Run BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Constrained random points on a circle step by step in the Run BASIC programming language

Table of Contents

Problem Statement

Generate 100 <x,y> coordinate pairs such that x and y are integers sampled from the uniform distribution with the condition that

10 ≤

x

2

y

2

≤ 15

{\displaystyle 10\leq {\sqrt {x^{2}+y^{2}}}\leq 15}

. Then display/plot them. The outcome should be a "fuzzy" circle. The actual number of points plotted may be less than 100, given that some pairs may be generated more than once. There are several possible approaches to accomplish this. Here are two possible algorithms.

  1. Generate random pairs of integers and filter out those that don't satisfy this condition:
  2. Precalculate the set of all possible points (there are 404 of them) and select randomly from this set.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Constrained random points on a circle step by step in the Run BASIC programming language

Source code in the run programming language

w = 320
h = 320
dim canvas(w,h)
for pts = 1 to 1000
  x = (rnd(1) * 31) - 15
  y = (rnd(1) * 31) - 15
  r = x * x + y * y
  if (r > 100) and (r < 225) then
    x = int(x * 10 + w/2)
    y = int(y * 10 + h/2)
    canvas(x,y) = 1
  end if
next pts    

' -----------------------------
' display the graphic
' -----------------------------
graphic #g, w,h
for x = 1 to w
  for y = 1 to h
     if canvas(x,y) = 1 then  #g "color green ; set "; x; " "; y else #g "color blue ; set "; x; " "; y
  next y
next x
render #g 
#g "flush"

  

You may also check:How to resolve the algorithm Optional parameters step by step in the Klingphix programming language
You may also check:How to resolve the algorithm Rename a file step by step in the Lua programming language
You may also check:How to resolve the algorithm Bitwise IO step by step in the C# programming language
You may also check:How to resolve the algorithm Factorial step by step in the HolyC programming language
You may also check:How to resolve the algorithm Variable-length quantity step by step in the D programming language