rand-bits

Crates.iorand-bits
lib.rsrand-bits
version0.1.1
sourcesrc
created_at2023-12-08 17:50:13.89787
updated_at2023-12-10 00:49:19.07183
descriptionRandom number generators with a fixed number of set bits (ones).
homepage
repositoryhttps://github.com/ventaquil/rand-bits
max_upload_size
id1062297
size16,286
kgolawski (ventaquil)

documentation

README

rand-bits

GitHub docs.rs MSRV deps.rs unsafe forbidden LICENSE

Random number generators with a fixed number of set bits (ones).

Setup

To use this crate, add the following entry to your Cargo.toml file in the dependencies section:

[dependencies]
rand-bits = "0.1.1"

Alternatively, you can use the cargo add subcommand:

cargo add rand-bits

Usage

use rand::thread_rng;
use rand_bits::RngBits;

let mut rng = thread_rng();
let x: u8 = rng.gen_bits(4); // generates a u8 with 4 set bits
assert_eq!(x.count_ones(), 4);
let y: u16 = rng.gen_bits(15); // generates a u16 with 15 set bits
assert_eq!(y.count_ones(), 15);
let z: u64 = rng.gen_bits(1); // generates a u64 with 1 set bits
assert_eq!(z.count_ones(), 1);

For more usage examples, refer to the documentation available at docs.rs.

License

This crate is licensed under the MIT License.

Commit count: 12

cargo fmt