[go: up one dir, main page]

Crate randomize[][src]

A dead simple to use randomization library for rust.

The library contains a trait for an RNG family, PCG, and two main implementations from that family: PCG32 and PCG64.

use randomize::{u64_from_time, PCG, PCG32};
let time64 = u64_from_time();
let gen = &mut PCG32::new(time64, time64);
println!("here's a u32 value: {}", gen.next_u32());

There are also a selection of functions that will automatically use a global, lazily initialized, mutex guarded PCG32 value to get you your results.

use randomize;
if randomize::any() {
  let x: u32 = randomize::any();
  println!("x is a u32: {}", x);
} else {
  let x: [char; 10] = randomize::any();
  println!("x is a 10 character array: {:?}", x);

  let i = randomize::any_in_range(0 .. x.len()).unwrap();
  println!("The character at index {} was {}", i, x[i]);
}

This library gives priority to ease of use rather than trying to cover all possible uses. If you want a totally comprehensive randomization library for all possible cases then you probably want the rand crate.

NOT FOR CRYPTOGRAPHIC PURPOSES.

Macros

make_compact_pcg32_type

Makes a variant of the PCG32 type with a custom inc value baked in.

make_compact_pcg64_type

Makes a variant of the PCG64 type with a custom inc value baked in.

Structs

AsciiCharacter

A Distribution for to get random ASCII characters.

F32ZeroToOneExclusive

A Distribution for 0.0 up to (but excluding) 1.0.

F32ZeroToOneInclusive

A Distribution for 0.0 up to and including 1.0.

PCG32

A permuted congruential generator with 32-bit output and per-generator stream selection.

PCG64

A permuted congruential generator with 64-bit output and per-generator stream selection.

PercentChance

A Distribution for a percent chance of true.

RandRangeInclusive32

An inclusive random range with u32 outputs.

RandRangeInclusiveUsize

An inclusive random range with usize outputs.

Constants

d4

A const for 1d4.

d6

A const for 1d6.

d8

A const for 1d8.

d10

A const for 1d10.

d12

A const for 1d12.

d20

A const for 1d20.

reciprocal_u32_max_float

A single step of the u32 range, as an f32.

reciprocal_u64_max_double

A single step of the u64 range, as an f64.

Traits

AnyRandom

A trait for types can can be generated in "any" state.

Distribution

A trait for types that represent some sort of special distribution other than what the AnyRandom instance does.

PCG

The trait for any type that's a form of permuted congruential generator.

Functions

any

Calls any on the global generator.

any_ascii

Samples the AsciiCharacter distribution with the global generator.

any_f32_zero_to_one_exclusive

Samples the F32ZeroToOneExclusive distribution with the global generator.

any_f32_zero_to_one_inclusive

Samples the F32ZeroToOneInclusive distribution with the global generator.

any_in_range

Constructs and uses a RandRangeInclusiveUsize based on the Range.

get_global_generator

Obtains a MutexGuard holding the global PCG32.

percent_chance

Samples the given PercentChance with the global generator.

u64_from_time

Returns a u64 value based on the current system clock value.