6 releases

0.1.5 Mar 11, 2023
0.1.4 Jul 16, 2022
0.1.3 Jun 1, 2022
0.1.1 Nov 20, 2021

#134 in Embedded development

Download history 5/week @ 2022-11-29 2/week @ 2022-12-06 6/week @ 2022-12-13 2/week @ 2022-12-20 39/week @ 2022-12-27 2/week @ 2023-01-03 23/week @ 2023-01-10 12/week @ 2023-01-17 22/week @ 2023-01-24 30/week @ 2023-01-31 28/week @ 2023-02-07 27/week @ 2023-02-14 47/week @ 2023-02-21 40/week @ 2023-02-28 70/week @ 2023-03-07 65/week @ 2023-03-14

224 downloads per month
Used in 2 crates

MIT/Apache

20KB
251 lines

rhai-rand - Package to Generate Random Numbers

License crates.io crates.io

Rhai logo

rhai-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.

Usage

Cargo.toml

[dependencies]
rhai-rand = "0.1"

Rhai script

// 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));

Rust source

// 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::<INT>("(rand() % 100).abs()")?;

    println!("Random number = {}", value);
}

API and Features

See the online documentation.

Dependencies

~4.5MB
~96K SLoC