How to resolve the algorithm Modified random distribution step by step in the Rust programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Modified random distribution step by step in the Rust programming language
Table of Contents
Problem Statement
Given a random number generator, (rng), generating numbers in the range 0.0 .. 1.0 called rgen, for example; and a function modifier(x) taking an number in the same range and generating the probability that the input should be generated, in the same range 0..1; then implement the following algorithm for generating random numbers to the probability given by function modifier:
Show your output here, on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Modified random distribution step by step in the Rust programming language
Source code in the rust programming language
use ndhistogram::{Histogram, ndhistogram, axis::Uniform};
use rand::Rng;
/// change x in [0.0, 1.0) to a split with minimum probability at 0.5
fn modifier(x: f64) -> f64 {
if x < 0.5 {
return 2.0 * (0.5 - &x);
} else {
return 2.0 * (&x - 0.5);
}
}
const WANTED: usize = 20_000;
fn main() {
let mut hist = ndhistogram!(Uniform::new(19, -0.0, 1.0));
let mut rng = rand::thread_rng();
for _ in 0.. WANTED {
loop {
let x: f64 = rng.gen::<f64>();
let y: f64 = rng.gen::<f64>();
if y < modifier(x) {
hist.fill(&f64::from(x));
break;
}
}
}
println!("{}", hist);
}
You may also check:How to resolve the algorithm Colorful numbers step by step in the Picat programming language
You may also check:How to resolve the algorithm Almost prime step by step in the FOCAL programming language
You may also check:How to resolve the algorithm Brownian tree step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Euler Math Toolbox programming language
You may also check:How to resolve the algorithm Thue-Morse step by step in the Excel programming language