🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

fastrand

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastrand - cargo Package Compare versions

Package version was removed
This package version has been unpublished, mostly likely due to security reasons
Comparing version
2.3.0
to
2.4.0
+531
Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "async-trait"
version = "0.1.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "autocfg"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
[[package]]
name = "bumpalo"
version = "3.19.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510"
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]]
name = "cc"
version = "1.2.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "755d2fce177175ffca841e9a06afdb2c4ab0f593d53b4dee48147dfaade85932"
dependencies = [
"find-msvc-tools",
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "fastrand"
version = "2.4.0"
dependencies = [
"getrandom",
"rand",
"wasm-bindgen-test",
"wyhash",
]
[[package]]
name = "find-msvc-tools"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8591b0bcc8a98a64310a2fae1bb3e9b8564dd10e381e6e28010fde8e8e8568db"
[[package]]
name = "futures-core"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
[[package]]
name = "futures-task"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988"
[[package]]
name = "futures-util"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [
"futures-core",
"futures-task",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
name = "getrandom"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd"
dependencies = [
"cfg-if",
"js-sys",
"libc",
"r-efi",
"wasip2",
"wasm-bindgen",
]
[[package]]
name = "itoa"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2"
[[package]]
name = "js-sys"
version = "0.3.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c942ebf8e95485ca0d52d97da7c5a2c387d0e7f0ba4c35e93bfcaee045955b3"
dependencies = [
"once_cell",
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.180"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc"
[[package]]
name = "libm"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de"
[[package]]
name = "memchr"
version = "2.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
[[package]]
name = "minicov"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4869b6a491569605d66d3952bcdf03df789e5b536e5f0cf7758a7f08a55ae24d"
dependencies = [
"cc",
"walkdir",
]
[[package]]
name = "nu-ansi-term"
version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [
"windows-sys",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
"libm",
]
[[package]]
name = "once_cell"
version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "oorandom"
version = "11.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
[[package]]
name = "pin-project-lite"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "proc-macro2"
version = "1.0.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "535d180e0ecab6268a3e718bb9fd44db66bbbc256257165fc699dadf70d16fe7"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a"
dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "rand"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
dependencies = [
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c"
dependencies = [
"getrandom",
]
[[package]]
name = "rustversion"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "serde"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
dependencies = [
"serde_core",
"serde_derive",
]
[[package]]
name = "serde_core"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.149"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
dependencies = [
"itoa",
"memchr",
"serde",
"serde_core",
"zmij",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "slab"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2ae44ef20feb57a68b23d846850f861394c2e02dc425a50098ae8c90267589"
[[package]]
name = "syn"
version = "2.0.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]]
name = "wasip2"
version = "1.0.2+wasi-0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9517f9239f02c069db75e65f174b3da828fe5f5b945c4dd26bd25d89c03ebcf5"
dependencies = [
"wit-bindgen",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64024a30ec1e37399cf85a7ffefebdb72205ca1c972291c51512360d90bd8566"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70a6e77fd0ae8029c9ea0063f87c46fde723e7d887703d74ad2616d792e51e6f"
dependencies = [
"cfg-if",
"futures-util",
"js-sys",
"once_cell",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "008b239d9c740232e71bd39e8ef6429d27097518b6b30bdf9086833bd5b6d608"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5256bae2d58f54820e6490f9839c49780dff84c65aeab9e772f15d5f0e913a55"
dependencies = [
"bumpalo",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f01b580c9ac74c8d8f0c0e4afb04eeef2acf145458e52c03845ee9cd23e3d12"
dependencies = [
"unicode-ident",
]
[[package]]
name = "wasm-bindgen-test"
version = "0.3.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45649196a53b0b7a15101d845d44d2dda7374fc1b5b5e2bbf58b7577ff4b346d"
dependencies = [
"async-trait",
"cast",
"js-sys",
"libm",
"minicov",
"nu-ansi-term",
"num-traits",
"oorandom",
"serde",
"serde_json",
"wasm-bindgen",
"wasm-bindgen-futures",
"wasm-bindgen-test-macro",
"wasm-bindgen-test-shared",
]
[[package]]
name = "wasm-bindgen-test-macro"
version = "0.3.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f579cdd0123ac74b94e1a4a72bd963cf30ebac343f2df347da0b8df24cdebed2"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "wasm-bindgen-test-shared"
version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8145dd1593bf0fb137dbfa85b8be79ec560a447298955877804640e40c2d6ea"
[[package]]
name = "web-sys"
version = "0.3.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "312e32e551d92129218ea9a2452120f4aabc03529ef03e4d0d82fb2780608598"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "winapi-util"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys",
]
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
[[package]]
name = "wit-bindgen"
version = "0.51.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5"
[[package]]
name = "wyhash"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca4d373340c479fd1e779f7a763acee85da3e423b1a9a9acccf97babcc92edbb"
dependencies = [
"rand_core",
]
[[package]]
name = "zerocopy"
version = "0.8.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "668f5168d10b9ee831de31933dc111a459c97ec93225beb307aed970d1372dfd"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c7962b26b0a8685668b671ee4b54d007a67d4eaf05fda79ac0ecf41e32270f1"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "zmij"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfcd145825aace48cff44a8844de64bf75feec3080e0aa5cdbde72961ae51a65"
+1
-1
{
"git": {
"sha1": "8419f8916f08c63572b4f7cbdc07cec94c1fc5ed"
"sha1": "1fd5bbb300d43e2f15dd0173f776f1a785a42667"
},
"path_in_vcs": ""
}

@@ -11,3 +11,3 @@ #![feature(test)]

fn shuffle_wyhash(b: &mut Bencher) {
let mut rng = WyRng::from_rng(thread_rng()).unwrap();
let mut rng = WyRng::from_rng(&mut rand::rng());
let mut x = (0..100).collect::<Vec<usize>>();

@@ -32,7 +32,7 @@ b.iter(|| {

fn u8_wyhash(b: &mut Bencher) {
let mut rng = WyRng::from_rng(thread_rng()).unwrap();
let mut rng = WyRng::from_rng(&mut rand::rng());
b.iter(|| {
let mut sum = 0u8;
for _ in 0..10_000 {
sum = sum.wrapping_add(rng.gen::<u8>());
sum = sum.wrapping_add(rng.random::<u8>());
}

@@ -57,7 +57,7 @@ sum

fn u32_wyhash(b: &mut Bencher) {
let mut rng = WyRng::from_rng(thread_rng()).unwrap();
let mut rng = WyRng::from_rng(&mut rand::rng());
b.iter(|| {
let mut sum = 0u32;
for _ in 0..10_000 {
sum = sum.wrapping_add(rng.gen::<u32>());
sum = sum.wrapping_add(rng.random::<u32>());
}

@@ -81,2 +81,19 @@ sum

#[bench]
fn f32_fastrand(b: &mut Bencher) {
let mut rng = fastrand::Rng::new();
b.iter(|| {
// f32 sum unrolled 2x to hide f32-add latency.
//
// Optimal amount of unrolling is somewhat sensitive to CPU and algorithm.
// Variously could be 2x, 3x, or 4x unrolling. On AArch64 and x86-64 on the
// current algorithm, 2x seems to be optimal on this benchmark.
let mut sum = 0.0;
for _ in 0..5_000 {
sum += rng.f32() + rng.f32();
}
sum
})
}
#[bench]
fn fill(b: &mut Bencher) {

@@ -83,0 +100,0 @@ let mut rng = fastrand::Rng::new();

@@ -14,5 +14,5 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO

edition = "2018"
rust-version = "1.36"
rust-version = "1.63"
name = "fastrand"
version = "2.3.0"
version = "2.4.0"
authors = ["Stjepan Glavina <stjepang@gmail.com>"]

@@ -46,2 +46,11 @@ build = false

[features]
alloc = []
default = ["std"]
js = [
"std",
"getrandom",
]
std = ["alloc"]
[lib]

@@ -64,29 +73,20 @@ name = "fastrand"

[dev-dependencies.getrandom]
version = "0.2"
version = "0.3.4"
[dev-dependencies.rand]
version = "0.8"
version = "0.9"
[dev-dependencies.wyhash]
version = "0.5"
version = "0.6"
[features]
alloc = []
default = ["std"]
js = [
"std",
"getrandom",
]
std = ["alloc"]
[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown"))'.dependencies.getrandom]
version = "0.2"
features = ["js"]
version = "0.3.4"
features = ["wasm_js"]
optional = true
[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown"))'.dev-dependencies.getrandom]
version = "0.2"
features = ["js"]
version = "0.3.4"
features = ["wasm_js"]
[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown"))'.dev-dependencies.wasm-bindgen-test]
version = "0.3"

@@ -0,1 +1,9 @@

# Version 2.4.0
- Bump MSRV to 1.63. (#104)
- Improve quality of f32/f64 generation. (#103)
- Add `f{32,64}_inclusive` and `Rng::f{32,64}_inclusive`. (#103)
- Make `Rng::with_seed` const. (#107)
- Update `getrandom` to 0.3. (#104)
# Version 2.3.0

@@ -2,0 +10,0 @@

@@ -9,3 +9,3 @@ //! A global, thread-local random number generator.

// Chosen by fair roll of the dice.
// Chosen by a fair roll of the dice.
const DEFAULT_RNG_SEED: u64 = 0xef6f79ed30ba75a;

@@ -180,2 +180,7 @@

/// Generates a random `f32` in range `0..=1`.
pub fn f32_inclusive() -> f32 {
with_rng(|r| r.f32_inclusive())
}
/// Generates a random `f64` in range `0..1`.

@@ -186,2 +191,7 @@ pub fn f64() -> f64 {

/// Generates a random `f64` in range `0..=1`.
pub fn f64_inclusive() -> f64 {
with_rng(|r| r.f64_inclusive())
}
/// Collects `amount` values at random from the iterable into a vector.

@@ -188,0 +198,0 @@ pub fn choose_multiple<I: IntoIterator>(source: I, amount: usize) -> Vec<I::Item> {

@@ -47,3 +47,3 @@ //! A simple and fast random number generator.

//!
//! Generate a random [`Vec`] or [`String`]:
//! Generate a random [`Vec`] or [`String`](alloc::string::String):
//!

@@ -90,3 +90,3 @@ //! ```

//!
//! For non-WASI WASM targets, there is additional sublety to consider when utilizing the global RNG.
//! For non-WASI WASM targets, there is additional subtlety to consider when utilizing the global RNG.
//! By default, `std` targets will use entropy sources in the standard library to seed the global RNG.

@@ -126,3 +126,2 @@ //! However, these sources are not available by default on WASM targets outside of WASI.

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
mod global_rng;

@@ -267,3 +266,3 @@

let low = match range.start_bound() {
Bound::Unbounded => core::$t::MIN,
Bound::Unbounded => $t::MIN,
Bound::Included(&x) => x,

@@ -274,3 +273,3 @@ Bound::Excluded(&x) => x.checked_add(1).unwrap_or_else(panic_empty_range),

let high = match range.end_bound() {
Bound::Unbounded => core::$t::MAX,
Bound::Unbounded => $t::MAX,
Bound::Included(&x) => x,

@@ -284,3 +283,3 @@ Bound::Excluded(&x) => x.checked_sub(1).unwrap_or_else(panic_empty_range),

if low == core::$t::MIN && high == core::$t::MAX {
if low == $t::MIN && high == $t::MAX {
self.$gen() as $t

@@ -299,3 +298,3 @@ } else {

#[must_use = "this creates a new instance of `Rng`; if you want to initialize the thread-local generator, use `fastrand::seed()` instead"]
pub fn with_seed(seed: u64) -> Self {
pub const fn with_seed(seed: u64) -> Self {
Rng(seed)

@@ -374,14 +373,63 @@ }

/// Generates a random `f32` in range `0..=1`.
#[inline]
pub fn f32_inclusive(&mut self) -> f32 {
// Generate a number in 0..2^63 then convert to f32 and multiply by 2^(-63).
//
// Even though we're returning f32, we still generate u64 internally to make
// it possible to return nonzero numbers as small as 2^(-63). If we only
// generated u32 internally, the smallest nonzero number we could return
// would be 2^(-32).
//
// The integer we generate is in 0..2^63 rather than 0..2^64 to improve speed
// on x86-64, which has efficient i64->float conversion (cvtsi2ss) but for
// which u64->float conversion must be implemented in software.
//
// There is still some remaining bias in the int-to-float conversion, because
// nonzero numbers <=2^(-64) are never generated, even though they are
// expressible in f32. However, at this point the bias in int-to-float conversion
// is no larger than the bias in the underlying WyRand generator: since it only
// has a 64-bit state, it necessarily already have biases of at least 2^(-64)
// probability.
//
// See e.g. Section 3.1 of Thomas, David B., et al. "Gaussian random number generators,
// https://www.doc.ic.ac.uk/~wl/papers/07/csur07dt.pdf, for background.
const MUL: f32 = 1.0 / (1u64 << 63) as f32;
(self.gen_u64() >> 1) as f32 * MUL
}
/// Generates a random `f32` in range `0..1`.
///
/// Function `f32_inclusive()` is a little simpler and faster, so default
/// to that if inclusive range is acceptable.
#[inline]
pub fn f32(&mut self) -> f32 {
let b = 32;
let f = core::f32::MANTISSA_DIGITS - 1;
f32::from_bits((1 << (b - 2)) - (1 << f) + (self.u32(..) >> (b - f))) - 1.0
loop {
let x = self.f32_inclusive();
if x < 1.0 {
return x;
}
}
}
/// Generates a random `f64` in range `0..=1`.
#[inline]
pub fn f64_inclusive(&mut self) -> f64 {
// See the comment in f32_inclusive() for more details.
const MUL: f64 = 1.0 / (1u64 << 63) as f64;
(self.gen_u64() >> 1) as f64 * MUL
}
/// Generates a random `f64` in range `0..1`.
///
/// Function `f64_inclusive()` is a little simpler and faster, so default
/// to that if inclusive range is acceptable.
#[inline]
pub fn f64(&mut self) -> f64 {
let b = 64;
let f = core::f64::MANTISSA_DIGITS - 1;
f64::from_bits((1 << (b - 2)) - (1 << f) + (self.u64(..) >> (b - f))) - 1.0
loop {
let x = self.f64_inclusive();
if x < 1.0 {
return x;
}
}
}

@@ -397,3 +445,2 @@

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn choose_multiple<I: IntoIterator>(&mut self, source: I, amount: usize) -> Vec<I::Item> {

@@ -400,0 +447,0 @@ // Adapted from: https://docs.rs/rand/latest/rand/seq/trait.IteratorRandom.html#method.choose_multiple

@@ -79,2 +79,113 @@ #[cfg(all(target_family = "wasm", not(target_os = "wasi")))]

#[test]
fn f32() {
let mut r = fastrand::Rng::with_seed(0);
let tiny = (-24.0f32).exp2();
let mut count_tiny_nonzero = 0;
let mut count_top_half = 0;
for _ in 0..100_000_000 {
let x = r.f32();
assert!((0.0..1.0).contains(&x));
if x > 0.0 && x < tiny {
count_tiny_nonzero += 1;
} else if x > 0.5 {
count_top_half += 1;
}
}
assert!(count_top_half >= 49_000_000);
assert!(count_tiny_nonzero > 0);
}
#[test]
fn f32_inclusive() {
let mut r = fastrand::Rng::with_seed(0);
let tiny = (-24.0f32).exp2();
let mut count_top_half = 0;
let mut count_tiny_nonzero = 0;
let mut count_one = 0;
for _ in 0..100_000_000 {
let x = r.f32_inclusive();
assert!((0.0..=1.0).contains(&x));
if x == 1.0 {
count_one += 1;
} else if x > 0.5 {
count_top_half += 1;
} else if x > 0.0 && x < tiny {
count_tiny_nonzero += 1;
}
}
assert!(count_top_half >= 49_000_000);
assert!(count_one > 0);
assert!(count_tiny_nonzero > 0);
}
#[test]
fn f64() {
let mut r = fastrand::Rng::with_seed(0);
let mut count_top_half = 0;
for _ in 0..100_000_000 {
let x = r.f64();
assert!((0.0..1.0).contains(&x));
if x > 0.5 {
count_top_half += 1;
}
}
assert!(count_top_half >= 49_000_000);
}
#[test]
fn f64_inclusive() {
let mut r = fastrand::Rng::with_seed(0);
let mut count_top_half = 0;
for _ in 0..100_000_000 {
let x = r.f64_inclusive();
assert!((0.0..=1.0).contains(&x));
if x > 0.5 {
count_top_half += 1;
}
}
assert!(count_top_half >= 49_000_000);
}
#[test]
fn digit() {
for base in 1..36 {
let result = fastrand::digit(base);
assert!(result.is_ascii_digit() || result.is_ascii_lowercase());
}
}
#[test]
fn global_rng_choice() {
let items = [1, 4, 9, 5, 2, 3, 6, 7, 8, 0];
for item in &items {
while fastrand::choice(&items).unwrap() != item {}
}
}
#[test]
fn global_rng_alphabetic() {
for _ in 0..1000 {
let result = fastrand::alphabetic();
assert!(result.is_ascii_alphabetic())
}
}
#[test]
fn global_rng_lowercase() {
for _ in 0..1000 {
let result = fastrand::lowercase();
assert!(result.is_ascii_lowercase())
}
}
#[test]
fn global_rng_uppercase() {
for _ in 0..1000 {
let result = fastrand::uppercase();
assert!(result.is_ascii_uppercase())
}
}
#[test]
fn fill() {

@@ -89,5 +200,24 @@ let mut r = fastrand::Rng::new();

assert_ne!(a, b);
let mut a = [0u8; 9];
let mut b = [0u8; 9];
r.fill(&mut a);
r.fill(&mut b);
assert_ne!(a, b);
}
#[test]
fn global_rng_fill() {
let mut a = [0u8; 64];
let mut b = [0u8; 64];
fastrand::fill(&mut a);
fastrand::fill(&mut b);
assert_ne!(a, b);
}
#[test]
fn rng() {

@@ -117,2 +247,26 @@ let mut r = fastrand::Rng::new();

#[test]
fn rng_digit() {
let mut rng = fastrand::Rng::new();
for base in 1..36 {
let result = rng.digit(base);
assert!(result.is_ascii_digit() || result.is_ascii_lowercase());
}
}
#[test]
#[should_panic]
fn rng_digit_panic_1() {
let mut rng = fastrand::Rng::new();
let _result = rng.digit(0);
}
#[test]
#[should_panic]
fn rng_digit_panic_2() {
let mut rng = fastrand::Rng::new();
let base = rng.u32(37..);
let _result = rng.digit(base);
}
#[test]
fn with_seed() {

@@ -136,2 +290,6 @@ let mut a = fastrand::Rng::with_seed(7);

}
let empty_elements: Vec<i32> = Vec::new();
let empty_result = a.choose_multiple(empty_elements, 5);
assert!(empty_result.is_empty());
}

@@ -148,1 +306,57 @@

}
#[test]
fn choice_empty() {
let mut rng = fastrand::Rng::new();
let data: Vec<i32> = Vec::new();
let result = rng.choice(data);
assert!(result.is_none());
}
#[test]
fn lowercase() {
let mut rng = fastrand::Rng::new();
for _ in 0..1000 {
let result = rng.lowercase();
assert!(result.is_ascii_lowercase())
}
}
#[test]
fn alphabetic() {
let mut rng = fastrand::Rng::new();
for _ in 0..1000 {
let result = rng.alphabetic();
assert!(result.is_ascii_alphabetic())
}
}
#[test]
fn uppercase() {
let mut rng = fastrand::Rng::new();
for _ in 0..1000 {
let result = rng.uppercase();
assert!(result.is_ascii_uppercase())
}
}
#[test]
#[should_panic]
fn char_panic() {
let mut rng = fastrand::Rng::new();
let _result = rng.char('z'..='a');
}
#[test]
fn char() {
use core::ops::Bound;
let mut rng = fastrand::Rng::new();
let result = rng.char(..);
assert!(result >= 0 as char && result <= core::char::MAX);
let result = rng.char((Bound::Excluded('0'), Bound::Excluded('9')));
for _ in 0..1000 {
assert!(result > '0' && result < '9');
}
}

Sorry, the diff of this file is not supported yet