Crates.io | rhai-rand |
lib.rs | rhai-rand |
version | 0.1.6 |
source | src |
created_at | 2021-11-19 20:15:28.748075 |
updated_at | 2023-10-25 08:57:35.908104 |
description | Random number package for Rhai |
homepage | https://rhai.rs |
repository | https://github.com/rhaiscript |
max_upload_size | |
id | 484597 |
size | 39,145 |
rhai-rand
- Package to Generate Random Numbersrhai-rand
is a Rhai package to provide random number generation using the rand
crate.
Rhai is an embedded scripting language and evaluation engine for Rust that gives a safe and easy way to add scripting to any application.
Cargo.toml
[dependencies]
rhai-rand = "0.1"
// Create random boolean
let decision = rand_bool();
if decision {
// Create random number
let random_value = rand();
print(`Random number = ${random_value}`);
} else {
print("Fixed number = 42");
}
// Create array
let a = [1, 2, 3, 4, 5];
// Shuffle it!
a.shuffle();
// Now the array is shuffled randomly!
print(a);
// Sample a random value from the array
print(a.sample());
// Or sample multiple values
print(a.sample(3));
// packages::Package implements `as_shared_module`
// which we need to register the RandomPackage
use rhai::{Engine, packages::Package};
use rhai_rand::RandomPackage;
// Create Rhai scripting engine
let mut engine = Engine::new();
// Create random number package and add the package into the engine
engine.register_global_module(RandomPackage::new().as_shared_module());
// Print 10 random numbers, each of which between 0-99!
for _ in 0..10 {
let value = engine.eval::<i64>("(rand() % 100).abs()")?;
println!("Random number = {}", value);
}
See the online documentation.