https://github.com/rhaiscript/rhai-rand
Random numbers generation package for Rhai.
https://github.com/rhaiscript/rhai-rand
lib random random-generation rhai rhai-rand scripting-language
Last synced: 3 months ago
JSON representation
Random numbers generation package for Rhai.
- Host: GitHub
- URL: https://github.com/rhaiscript/rhai-rand
- Owner: rhaiscript
- License: apache-2.0
- Created: 2021-11-08T06:29:31.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-06-04T07:16:04.000Z (over 2 years ago)
- Last Synced: 2023-06-04T09:07:53.899Z (over 2 years ago)
- Topics: lib, random, random-generation, rhai, rhai-rand, scripting-language
- Language: Rust
- Homepage: https://crates.io/crates/rhai-rand
- Size: 41 KB
- Stars: 6
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE.txt
Awesome Lists containing this project
README
`rhai-rand` - Package to Generate Random Numbers
===============================================[](https://github.com/license/rhaiscript/rhai-rand)
[](https://crates.io/crates/rhai-rand/)
[](https://crates.io/crates/rhai-rand/)[](https://rhai.rs)
`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`
```toml
[dependencies]
rhai-rand = "0.1"
```### [Rhai] script
```js
// 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
```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::("(rand() % 100).abs()")?;println!("Random number = {}", value);
}
```API and Features
----------------See the online documentation.
[Rhai]: https://rhai.rs
[`rand`]: https://crates.io/crates/rand