7个版本
0.1.6 | 2023年10月25日 |
---|---|
0.1.5 | 2023年3月11日 |
0.1.4 | 2022年7月16日 |
0.1.3 | 2022年6月1日 |
0.1.1 | 2021年11月20日 |
#136 在 嵌入式开发
523 每月下载量
在 4 个包中使用了(3 个直接使用)
23KB
279 代码行
rhai-rand
- 生成随机数的包
rhai-rand
是一个 Rhai 包,使用 rand
包提供随机数生成。
Rhai 是一个为 Rust 提供嵌入式脚本语言和评估引擎的库,它为任何应用程序添加脚本提供了一种安全和简单的方式。
用法
Cargo.toml
[dependencies]
rhai-rand = "0.1"
Rhai 脚本
// 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 源代码
// 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 和功能
请参阅在线文档。
依赖项
~4–5.5MB
~106K SLoC