Faster and static random number generator

This commit is contained in:
Jonathan Flueren 2022-06-01 16:42:49 +02:00
parent 0632c60989
commit b82a323642
5 changed files with 18 additions and 13 deletions

12
Cargo.lock generated
View file

@ -97,21 +97,21 @@ dependencies = [
[[package]] [[package]]
name = "jpeg-decoder" name = "jpeg-decoder"
version = "0.2.5" version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be7ef4b99870f0c9f2fc2f20dbef72707e2bcca675bb9196734cf433e999b0c5" checksum = "9478aa10f73e7528198d75109c8be5cd7d15fb530238040148d5f9a22d4c5b3b"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.125" version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b" checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
[[package]] [[package]]
name = "miniz_oxide" name = "miniz_oxide"
version = "0.5.1" version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2b29bd4bc3f33391105ebee3589c19197c4271e3e5a9ec9bfe8127eeff8f082" checksum = "6f5c75688da582b8ffc1f1799e9db273f32133c49e048f614d22ec3256773ccc"
dependencies = [ dependencies = [
"adler", "adler",
] ]

View file

@ -7,4 +7,4 @@ edition = "2021"
[dependencies] [dependencies]
image = { version = "0.24.2", default-features = false, features = ["jpeg", "png", "pnm"] } image = { version = "0.24.2", default-features = false, features = ["jpeg", "png", "pnm"] }
rand = "0.8.5" rand = { version = "0.8.5", features = ["small_rng"] }

View file

@ -18,9 +18,9 @@ pub fn put_color(img: &mut RgbImage, pixel_color: &Color, x: u32, y: u32, sample
let mut b = pixel_color.z(); let mut b = pixel_color.z();
let scale = 1.0 / samples_per_pixel as f64; let scale = 1.0 / samples_per_pixel as f64;
r *= scale; r = (scale*r).sqrt();
g *= scale; g = (scale*g).sqrt();
b *= scale; b = (scale*b).sqrt();
img.put_pixel( img.put_pixel(
x, x,

View file

@ -26,7 +26,7 @@ fn ray_color(r: &Ray, world: &HittableList, depth: u32) -> Color {
return Color::new(0.0,0.0,0.0); return Color::new(0.0,0.0,0.0);
} }
if world.hit(r, 0.0, f64::INFINITY, &mut rec) { if world.hit(r, 0.001, f64::INFINITY, &mut rec) {
let target = rec.p + rec.normal + Vec3::random_in_unit_sphere(); let target = rec.p + rec.normal + Vec3::random_in_unit_sphere();
return 0.5 * ray_color(&Ray::new(rec.p, target - rec.p), world, depth-1); return 0.5 * ray_color(&Ray::new(rec.p, target - rec.p), world, depth-1);
} }

View file

@ -1,8 +1,13 @@
use rand::Rng; use rand::prelude::*;
use std::cell::RefCell;
thread_local! {
static RNG: RefCell<SmallRng> = RefCell::new(SmallRng::from_entropy())
}
/// generates random number 0<= x < 1 /// generates random number 0<= x < 1
pub fn random_f64() -> f64 { pub fn random_f64() -> f64 {
rand::thread_rng().gen_range(0.0..1.0) RNG.with(|rng| (*rng.borrow_mut()).gen())
} }
pub fn random_rng(min: f64, max: f64) -> f64 { pub fn random_rng(min: f64, max: f64) -> f64 {