Generate Random Values
Generate Random Numbers
Generates random numbers with help of the random-number generator rand::Rng
. Each thread has an initialized generator. Integers are uniformly distributed over the range of the type, and floating point numbers are uniformly distributed from 0 up to but not including 1.
Generate Random Numbers Within a Range
Generates a random value within half-open [0, 10)
range (not including 10
) with rand::Rng::gen_range
⮳ range.
rand::distributions::uniform::Uniform
can obtain values with uniform distribution. This has the same effect, but may be faster when repeatedly generating numbers in the same range.
Generate Random Numbers Within a Given Distribution
By default, random numbers in the rand
⮳ crate have uniform distribution⮳. The rand_distr
⮳ crate provides other kinds of distributions. To use them, you instantiate a distribution, then sample from that distribution using rand::distributions::Distribution::sample
⮳ with help of a random-number generator rand::Rng
⮳. The distributions available are documented here⮳. An example using the rand_distr::Normal
⮳ distribution is shown below.
Generate Random Values of a Custom Type
Randomly generates a tuple (i32, bool, f64)
and variable of user defined type Point
. Implements the rand::distributions::Distribution
⮳ trait on type Point for rand::distributions::Standard
⮳ trait in order to allow random generation.
Create Random Passwords from a Set of Alphanumeric Characters
Randomly generates a string of given length ASCII characters in the range A-Z, a-z, 0-9
, with rand::distributions::Alphanumeric
⮳ sample.
Create Random Passwords from a Set of User-defined Characters
Randomly generates a string of given length ASCII characters with custom user-defined bytestring, with rand::Rng::gen_range
.
fastrand
fastrand
⮳ is a simple and fast random number generator. No dependencies, non-cryptographically secure random numbers, lower complexity than rand
⮳.