pub struct Random {
pub state: State,
}
Expand description
An implementation of Unity’s seeded PRNG, which aims to be faster than
.NET’s System.Random
.
Fields§
§state: State
Gets or sets the full internal state of the random number generator.
Implementations§
Source§impl Random
impl Random
Sourcepub fn init_state(&mut self, seed: i32)
pub fn init_state(&mut self, seed: i32)
(Re)-initializes the PRNG with a given seed.
Sourcepub fn range_int(&mut self, min: i32, max: i32) -> i32
pub fn range_int(&mut self, min: i32, max: i32) -> i32
Returns a random i32
within [min..max]
.
Minimum is inclusive, maximum is exclusive.
Sourcepub fn range_float(&mut self, min: f32, max: f32) -> f32
pub fn range_float(&mut self, min: f32, max: f32) -> f32
Returns a random f32
within [min..max]
(range is inclusive).
Sourcepub fn inside_unit_circle(&mut self) -> (f32, f32)
pub fn inside_unit_circle(&mut self) -> (f32, f32)
Returns a random point inside or on a circle with radius 1.0.
Note that the probability space includes the perimeter of the circle because next_f32
,
which is inclusive to 1.0, is used to acquire a random radius.
Sourcepub fn on_unit_sphere(&mut self) -> (f32, f32, f32)
pub fn on_unit_sphere(&mut self) -> (f32, f32, f32)
Returns a random point on the surface of a sphere with radius 1.0.
Sourcepub fn inside_unit_sphere(&mut self) -> (f32, f32, f32)
pub fn inside_unit_sphere(&mut self) -> (f32, f32, f32)
Returns a random point inside or on a sphere with radius 1.0.
Note that the probability space includes the surface of the sphere because next_f32
,
which is inclusive to 1.0, is used to acquire a random radius.
Sourcepub fn rotation(&mut self) -> (f32, f32, f32, f32)
pub fn rotation(&mut self) -> (f32, f32, f32, f32)
Returns a random rotation.
Randomize the x, y, z, and w of a Quaternion each to [-1.0..1.0]
(inclusive)
via Range and normalize the result.
See also rotation_uniform
for a slower but higher quality algorithm.
Sourcepub fn rotation_uniform(&mut self) -> (f32, f32, f32, f32)
pub fn rotation_uniform(&mut self) -> (f32, f32, f32, f32)
Returns a random rotation with uniform distribution.
Employs Hopf fibration to return a random Quaternion within a uniformly distributed selection space.
Gives higher quality results compared to the more naive approach employed by rotation, though at a 40% performance cost.
Sourcepub fn color(&mut self) -> (f32, f32, f32, f32)
pub fn color(&mut self) -> (f32, f32, f32, f32)
Generates a random RGBA color.
This may produce inaccurate results due to an issue with .NET versions before 5.x.
Unity uses a custom build of Mono, which is based on an older .NET version.
Sourcepub fn color_h(&mut self, hue_min: f32, hue_max: f32) -> (f32, f32, f32, f32)
pub fn color_h(&mut self, hue_min: f32, hue_max: f32) -> (f32, f32, f32, f32)
Generates a random RGBA color from hue ranges.
This may produce inaccurate results due to an issue with .NET versions before 5.x.
Unity uses a custom build of Mono, which is based on an older .NET version.
Sourcepub fn color_hs(
&mut self,
hue_min: f32,
hue_max: f32,
saturation_min: f32,
saturation_max: f32,
) -> (f32, f32, f32, f32)
pub fn color_hs( &mut self, hue_min: f32, hue_max: f32, saturation_min: f32, saturation_max: f32, ) -> (f32, f32, f32, f32)
Generates a random RGBA color from hue and saturation ranges.
This may produce inaccurate results due to an issue with .NET versions before 5.x.
Unity uses a custom build of Mono, which is based on an older .NET version.
Sourcepub fn color_hsv(
&mut self,
hue_min: f32,
hue_max: f32,
saturation_min: f32,
saturation_max: f32,
value_min: f32,
value_max: f32,
) -> (f32, f32, f32, f32)
pub fn color_hsv( &mut self, hue_min: f32, hue_max: f32, saturation_min: f32, saturation_max: f32, value_min: f32, value_max: f32, ) -> (f32, f32, f32, f32)
Generates a random RGBA color from HSV ranges.
This may produce inaccurate results due to an issue with .NET versions before 5.x.
Unity uses a custom build of Mono, which is based on an older .NET version.
Sourcepub fn color_hsva(
&mut self,
hue_min: f32,
hue_max: f32,
saturation_min: f32,
saturation_max: f32,
value_min: f32,
value_max: f32,
alpha_min: f32,
alpha_max: f32,
) -> (f32, f32, f32, f32)
pub fn color_hsva( &mut self, hue_min: f32, hue_max: f32, saturation_min: f32, saturation_max: f32, value_min: f32, value_max: f32, alpha_min: f32, alpha_max: f32, ) -> (f32, f32, f32, f32)
Generates a random RGBA color from HSV and alpha ranges.
This may produce inaccurate results due to an issue with .NET versions before 5.x.
Unity uses a custom build of Mono, which is based on an older .NET version.