How to resolve the algorithm Constrained random points on a circle step by step in the Rust 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 Rust 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 Rust programming language
Source code in the rust programming language
extern crate rand;
use rand::Rng;
const POINTS_N: usize = 100;
fn generate_point<R: Rng>(rng: &mut R) -> (i32, i32) {
loop {
let x = rng.gen_range(-15, 16); // exclusive
let y = rng.gen_range(-15, 16);
let r2 = x * x + y * y;
if r2 >= 100 && r2 <= 225 {
return (x, y);
}
}
}
fn filtering_method<R: Rng>(rng: &mut R) {
let mut rows = [[" "; 62]; 31];
// Generate points
for _ in 0..POINTS_N {
let (x, y) = generate_point(rng);
rows[(y + 15) as usize][(x + 15) as usize * 2] = "*";
}
// draw the points
for row in &rows {
println!("{}", row.concat());
}
}
fn precalculating_method<R: Rng>(rng: &mut R) {
// Generate all possible points
let mut possible_points = Vec::with_capacity(404);
for y in -15..=15 {
for x in -15..=15 {
let r2 = x * x + y * y;
if r2 >= 100 && r2 <= 225 {
possible_points.push((x, y));
}
}
}
// A truncated Fisher-Yates shuffle
let len = possible_points.len();
for i in (len - POINTS_N..len).rev() {
let j = rng.gen_range(0, i + 1);
possible_points.swap(i, j);
}
// turn the selected points into "pixels"
let mut rows = [[" "; 62]; 31];
for &(x, y) in &possible_points[len - POINTS_N..] {
rows[(y + 15) as usize][(x + 15) as usize * 2] = "*";
}
// draw the "pixels"
for row in &rows {
println!("{}", row.concat());
}
}
fn main() {
let mut rng = rand::weak_rng();
filtering_method(&mut rng);
precalculating_method(&mut rng);
}
You may also check:How to resolve the algorithm Parsing/RPN calculator algorithm step by step in the Sidef programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Picat programming language
You may also check:How to resolve the algorithm Grayscale image step by step in the C programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the zkl programming language