7 releases

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

#118 in Embedded development

Download history 59/week @ 2024-07-24 84/week @ 2024-07-31 66/week @ 2024-08-07 73/week @ 2024-08-14 114/week @ 2024-08-21 96/week @ 2024-08-28 402/week @ 2024-09-04 111/week @ 2024-09-11 122/week @ 2024-09-18 49/week @ 2024-09-25 46/week @ 2024-10-02 49/week @ 2024-10-09 46/week @ 2024-10-16 57/week @ 2024-10-23 177/week @ 2024-10-30 30/week @ 2024-11-06

314 downloads per month
Used in 4 crates (3 directly)

MIT/Apache

23KB
279 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::<i64>("(rand() % 100).abs()")?;

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

API and Features

See the online documentation.

Dependencies

~6.5MB
~123K SLoC