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

On crates.io: rhai-rand

On GitHub: rhaiscript/rhai-rand

Package name: RandomPackage

Dependency

┌────────────┐
│ Cargo.toml │
└────────────┘

[dependencies]
rhai = "1.2.0"
rhai-rand = "0.1"

Load Package into Engine


#![allow(unused)]
fn main() {
use rhai::Engine;
use rhai::packages::Package;    // needed for 'as_shared_module'
use rhai_rand::RandomPackage;

let mut engine = Engine::new();

// Create new 'RandomPackage' instance
let random = RandomPackage::new();

// Load the package
engine.register_global_module(random.as_shared_module());
}

Features

FeatureDescriptionShould not be used with Rhai feature
floatenables random floating-point number generationno_float
arrayenables methods for arraysno_index

Package Functions

The following functions are defined:

FunctionReturn valueFeatureDescription
randINTgenerates a random number
rand_floatFLOATfloatgenerates a random floating-point number between 0.0 and 1.0
rand_boolboolgenerates a random boolean

Arrays

The following methods are defined for arrays:

MethodParameter(s)Return valueFeatureDescription
shufflenonearrayshuffles the items in the array
samplenoneDynamicarrayreturns a random item from the array
samplenumber of items to sample (empty if ≤ 0, all if ≥ length)Arrayarrayreturns a non-repeating shuffled random sample of items from the array