How to resolve the algorithm Constrained random points on a circle step by step in the F# 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 F# 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.
- Generate random pairs of integers and filter out those that don't satisfy this condition:
- 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 F# programming language
Source code in the fsharp programming language
module CirclePoints =
let main args =
let rnd = new System.Random()
let rand size = rnd.Next(size) - size/2
let size = 30
let gen n =
let rec f (x,y) =
let t = (int (sqrt (float (x*x + y*y)) ))
if 10 <= t && t <= 15 then (x,y) else f (rand size, rand size)
f (rand size, rand size)
let plot = Array.init 100 (fun n -> gen n)
for row in 0 .. size-1 do
let chars = Array.create (size+1) ' '
Array.choose (fun (x,y) -> if y = (row-size/2) then Some(x) else None) plot
|> Array.iter (fun x -> chars.[x+size/2] <- 'o')
printfn "%s" (new string(chars))
0
#if INTERACTIVE
CirclePoints.main fsi.CommandLineArgs
#else
[<EntryPoint>]
let main args = CirclePoints.main args
#endif
You may also check:How to resolve the algorithm List comprehensions step by step in the Wrapl programming language
You may also check:How to resolve the algorithm Factorial step by step in the RPL programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Validate International Securities Identification Number step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Non-decimal radices/Input step by step in the ALGOL 68 programming language