rhai-rand: Random Number Generation, Shuffling and Sampling

rhai-rand is an independent Rhai package that provides:

  • random number generation using the rand crate
  • array shuffling and sampling

Documentation

See https://docs.rs/rhai-rand for the list of functions.

On crates.io: rhai-rand

On GitHub: rhaiscript/rhai-rand

Package name: RandomPackage

Dependency

Cargo.toml:

[dependencies] rhai = "1.21.0" rhai-rand = "0.1" # use rhai-rand crate

Load Package into Engine

use rhai::Engine; use rhai::packages::Package; // needed for 'Package' trait use rhai_rand::RandomPackage; let mut engine = Engine::new(); // Create new 'RandomPackage' instance let random = RandomPackage::new(); // Load the package into the `Engine` random.register_into_engine(&mut engine);

Features

FeatureDescriptionDefault?Should not be used with Rhai feature
floatenables random floating-point number generationyesno_float
arrayenables methods for arraysyesno_index
metadataenables functions metadata (turns on metadata in Rhai)no

Example: Working with no_float in Rhai

Cargo.toml:

[dependencies] # Rhai is set for 'no_float', meaning no floating-point support rhai = { version="1.21.0", features = ["no_float"] } # Use 'default-features = false' to clear defaults, then only add 'array' rhai-rand = { version="0.1", default-features = false, features = ["array"] }