Skip to main content

Rng

Struct Rng 

Source
pub struct Rng(/* private fields */);
Expand description

Deterministic xorshift64* PRNG. Avoids a dependency and keeps scene generation identical on every platform (important for WASM + tests).

Clone is deliberate: cloning captures the exact stream position, which is how a speculative draw can be replayed or a substream forked at a known point.

Implementations§

Source§

impl Rng

Source

pub fn new(seed: u64) -> Self

A generator from a seed.

Every seed but zero is used as given; xorshift is stuck at zero forever, so that one value is replaced.

Source

pub fn for_index(seed: u64, index: u64) -> Rng

An independent stream for one piece of work, identified by index.

Stateless and order-free: the stream for index 10 000 is what it is whether or not index 3 was ever drawn. That is what lets a trace be parallel and bit-reproducible at the same time, and it is why every stochastic loop should seed per item rather than share one generator.

Both arguments are mixed, so (seed, index) and (index, seed) differ and adjacent indices do not produce correlated streams.

Source

pub fn split(&mut self) -> Rng

Fork a child stream and advance this one past it.

For nesting that has no natural index — a recursive bounce that needs its own sampling without disturbing the caller’s sequence. Where an index exists, prefer Rng::for_index: splitting is still order-dependent.

Source

pub fn unit(&mut self) -> f64

Uniform in [0, 1).

Source

pub fn range(&mut self, lo: f64, hi: f64) -> f64

Uniform in [lo, hi). One draw from the stream, whatever the bounds.

Source

pub fn in_disc(&mut self, radius: f64) -> (f64, f64)

Uniform point inside a disc of the given radius.

Source

pub fn on_sphere(&mut self) -> DVec3

Uniform direction on the unit sphere.

Source

pub fn on_hemisphere(&mut self, normal: DVec3) -> DVec3

Uniform direction on the hemisphere about normal.

Source

pub fn cosine_hemisphere(&mut self, normal: DVec3) -> DVec3

Cosine-weighted direction about normal — the Lambertian scatter.

A matte surface does not spray light evenly over the hemisphere: it sends it in proportion to the cosine of the angle from the normal, which is why it looks equally bright from every direction. Sampled by Malley’s method (a uniform disc lifted onto the hemisphere), so the cosine weight is in the distribution and the estimator needs no correction factor.

Source

pub fn gaussian(&mut self) -> f64

A standard normal deviate, mean 0 and variance 1.

Read noise, mechanical jitter, thermal fluctuation, Brownian motion: the noise in a simulation is Gaussian far more often than it is uniform. Plain Box-Muller rather than the polar form, because it draws exactly two numbers every time — a rejection loop would make stream consumption depend on the values drawn, and that is a needless dependency in something whose whole job is being predictable.

Source

pub fn normal(&mut self, mean: f64, std_dev: f64) -> f64

A normal deviate with the given mean and standard deviation.

Source

pub fn poisson(&mut self, mean: f64) -> u64

A Poisson deviate: the number of independent events that happened, when the expected number was mean.

The distribution of counting things that arrive at random — photons on a detector, decays in a sample, molecules crossing a boundary. Its defining property is that the variance equals the mean, so the noise on a count of N is √N and the signal-to-noise ratio of counting improves only as the square root of how long you count. That is not a limitation of any instrument; it is what counting is.

Two methods, chosen by the mean rather than by the draw, so stream consumption stays a function of the inputs:

  • Below 30, inverse transform from a single uniform. Walking the cumulative distribution costs O(mean) time and exactly one draw, where the textbook product-of-uniforms method would consume a variable number and make the stream depend on the values it produced.
  • At 30 and above, a rounded normal. The skew there is 0.18 and the tail error is under a percent, which is far below any detector’s calibration — and the exact method’s cost grows with the mean while its benefit does not.

Trait Implementations§

Source§

impl Clone for Rng

Source§

fn clone(&self) -> Rng

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Rng

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Rng

Source§

impl Hash for Rng

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Rng

Source§

fn eq(&self, other: &Rng) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Rng

Auto Trait Implementations§

§

impl Freeze for Rng

§

impl RefUnwindSafe for Rng

§

impl Send for Rng

§

impl Sync for Rng

§

impl Unpin for Rng

§

impl UnsafeUnpin for Rng

§

impl UnwindSafe for Rng

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.