rhai-rand
: Random Number Generation, Shuffling and Sampling
rhai-rand
is an independent Rhai package that provides:
On crates.io
: rhai-rand
On GitHub
: rhaiscript/rhai-rand
Package name: RandomPackage
Dependency
Cargo.toml
:
[dependencies]
rhai = "1.7.0"
rhai-rand = "0.1" # use rhai-rand crate
Load Package into Engine
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
Feature | Description | Default? | Should not be used with Rhai feature |
---|---|---|---|
float | enables random floating-point number generation | yes | no_float |
array | enables methods for arrays | yes | no_index |
metadata | enables 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.7.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"] }
Package Functions
The following functions are defined.
Function | Return type | Feature | Description |
---|---|---|---|
rand() | INT | generates a random number | |
rand(start..end) | INT | generates a random number within the exclusive range start..end | |
rand(start..=end) | INT | generates a random number within the inclusive range start..=end | |
rand_float() | FLOAT | float | generates a random floating-point number between 0.0 and 1.0 (exclusive) |
rand_bool() | bool | generates a random boolean | |
rand_bool(p) | bool | float | generates a random boolean with the probability p of being true |
Arrays
The following methods are defined for arrays (requires the array
feature).
Method | Parameter(s) | Return type | Description |
---|---|---|---|
shuffle | none | shuffles the items in the array | |
sample | none | Dynamic | returns a random item from the array |
sample | number of items to sample (empty if ≤ 0, all if ≥ length) | Array | returns a non-repeating shuffled random sample of items from the array |