How to resolve the algorithm Constrained random points on a circle step by step in the Common Lisp 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 Common Lisp 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 Common Lisp programming language
Source code in the common programming language
(flet ((good-p (x y) (<= 100 (+ (* x x) (* y y)) 255)))
(loop with x with y with cnt = 0
with scr = (loop repeat 31 collect (loop repeat 31 collect " "))
while (< cnt 100)
do (when (good-p (- (setf x (random 31)) 15)
(- (setf y (random 31)) 15))
(setf (elt (elt scr y) x) "@ ")
(incf cnt))
finally (mapc #'(lambda (row) (format t "~{~a~^~}~%" row)) scr)))
You may also check:How to resolve the algorithm Sisyphus sequence step by step in the Phix programming language
You may also check:How to resolve the algorithm Gamma function step by step in the Scilab programming language
You may also check:How to resolve the algorithm Cuban primes step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the Factor programming language
You may also check:How to resolve the algorithm XML/DOM serialization step by step in the F# programming language