Movatterモバイル変換


[0]ホーム

URL:


Docs.rs

Craterand

Source
Expand description

Utilities for random number generation

Rand provides utilities to generate random numbers, to convert them touseful types and distributions, and some randomness-related algorithms.

§Quick Start

// The prelude import enables methods we use below, specifically// Rng::random, Rng::sample, SliceRandom::shuffle and IndexedRandom::choose.userand::prelude::*;// Get an RNG:letmutrng = rand::rng();// Try printing a random unicode code point (probably a bad idea)!println!("char: '{}'", rng.random::<char>());// Try printing a random alphanumeric value instead!println!("alpha: '{}'", rng.sample(rand::distr::Alphanumeric)aschar);// Generate and shuffle a sequence:letmutnums: Vec<i32> = (1..100).collect();nums.shuffle(&mutrng);// And take a random pick (yes, we didn't need to shuffle first!):let _= nums.choose(&mutrng);

§The Book

For the user guide and further documentation, please readThe Rust Rand Book.

Modules§

distr
Generating random samples from probability distributions
prelude
Convenience re-export of common members
rngs
Random number generators and adapters
seq
Sequence-related functionality

Traits§

CryptoRng
A marker trait used to indicate that anRngCore implementation issupposed to be cryptographically secure.
Fill
Types which may be filled with random data
Rng
User-level interface for RNGs
RngCore
Implementation-level interface for RNGs
SeedableRng
A random number generator that can be explicitly seeded.
TryCryptoRng
A marker trait used to indicate that aTryRngCore implementation issupposed to be cryptographically secure.
TryRngCore
A potentially fallible variant ofRngCore

Functions§

fillthread_rng
Fill any type implementingFill with random data
randomthread_rng
Generate a random value using the thread-local random number generator.
random_boolthread_rng
Return a bool with a probabilityp of being true.
random_iterthread_rng
Return an iterator overrandom() variates
random_rangethread_rng
Generate a random value in the given range using the thread-local random number generator.
random_ratiothread_rng
Return a bool with a probability ofnumerator/denominator of beingtrue.
rngthread_rng
Access a fast, pre-initialized generator
thread_rngDeprecatedthread_rng
Access the thread-local generator

[8]ページ先頭

©2009-2025 Movatter.jp