-
Notifications
You must be signed in to change notification settings - Fork 94
Add the rand
module
#100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add the rand
module
#100
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#![allow(dead_code)] | ||
//! The native `rand` module for the [Rune Language]. | ||
//! | ||
//! [Rune Language]: https://rune-rs.github.io | ||
//! | ||
//! ## Usage | ||
//! | ||
//! Add the following to your `Cargo.toml`: | ||
//! | ||
//! ```toml | ||
//! rune-modules = {version = "0.6.16", features = ["rand"]} | ||
//! ``` | ||
//! | ||
//! Install it into your context: | ||
//! | ||
//! ```rust | ||
//! # fn main() -> runestick::Result<()> { | ||
//! let mut context = runestick::Context::with_default_modules()?; | ||
//! context.install(&rune_modules::rand::module()?)?; | ||
//! # Ok(()) | ||
//! # } | ||
//! ``` | ||
//! | ||
//! Use it in Rune: | ||
//! | ||
//! ```rust,ignore | ||
//! fn main() { | ||
//! let rng = rand::WyRand::new(); | ||
//! let rand_int = rng.int(); | ||
//! println(`Random int: {rand_int}`); | ||
//! let rand_int_range = rng.int_range(-100, 100); | ||
//! println(`Random int between -100 and 100: {rand_int_range}`); | ||
//! } | ||
//! ``` | ||
|
||
use nanorand::RNG; | ||
use runestick::{Any, ContextError, Module, Value}; | ||
|
||
/// Construct the `rand` module. | ||
pub fn module() -> Result<Module, ContextError> { | ||
let mut module = Module::new(&["rand"]); | ||
|
||
module.ty::<WyRand>()?; | ||
module.function(&["WyRand", "new"], WyRand::new)?; | ||
module.function(&["WyRand", "new_seed"], WyRand::new_seed)?; | ||
module.inst_fn("int", WyRand::int)?; | ||
module.inst_fn("int_range", WyRand::int_range)?; | ||
|
||
module.ty::<Pcg64>()?; | ||
module.function(&["Pcg64", "new"], Pcg64::new)?; | ||
module.function(&["Pcg64", "new_seed"], Pcg64::new_seed)?; | ||
module.inst_fn("int", Pcg64::int)?; | ||
module.inst_fn("int_range", Pcg64::int_range)?; | ||
|
||
module.function(&["int"], int)?; | ||
module.function(&["int_range"], int_range)?; | ||
|
||
Ok(module) | ||
} | ||
|
||
#[derive(Any)] | ||
struct WyRand { | ||
inner: nanorand::WyRand, | ||
} | ||
|
||
impl WyRand { | ||
/// Create a new RNG instance. | ||
fn new() -> Self { | ||
Self { | ||
inner: nanorand::WyRand::new(), | ||
} | ||
} | ||
|
||
/// Create a new RNG instance, using a custom seed. | ||
fn new_seed(seed: i64) -> Self { | ||
Self { | ||
inner: nanorand::WyRand::new_seed(seed as u64), | ||
} | ||
} | ||
|
||
/// Generate a random integer | ||
fn int(&mut self) -> Value { | ||
Value::Integer(self.inner.generate::<i64>()) | ||
} | ||
|
||
/// Generate a random integer within the specified range | ||
fn int_range(&mut self, lower: i64, upper: i64) -> Value { | ||
Value::Integer(self.inner.generate_range::<i64>(lower, upper)) | ||
} | ||
} | ||
|
||
#[derive(Any)] | ||
struct Pcg64 { | ||
inner: nanorand::Pcg64, | ||
} | ||
|
||
impl Pcg64 { | ||
/// Create a new RNG instance. | ||
fn new() -> Self { | ||
Self { | ||
inner: nanorand::Pcg64::new(), | ||
} | ||
} | ||
|
||
/// Create a new RNG instance, using a custom seed. | ||
fn new_seed(seed: i64) -> Self { | ||
Self { | ||
inner: nanorand::Pcg64::new_seed(seed as u128), | ||
} | ||
} | ||
|
||
/// Generate a random integer | ||
fn int(&mut self) -> Value { | ||
Value::Integer(self.inner.generate::<i64>()) | ||
} | ||
|
||
/// Generate a random integer within the specified range | ||
fn int_range(&mut self, lower: i64, upper: i64) -> Value { | ||
Value::Integer(self.inner.generate_range::<i64>(lower, upper)) | ||
} | ||
} | ||
|
||
fn int() -> runestick::Result<Value> { | ||
Ok(Value::Integer(nanorand::WyRand::new().generate::<i64>())) | ||
} | ||
|
||
fn int_range(lower: i64, upper: i64) -> runestick::Result<Value> { | ||
Ok(Value::Integer( | ||
nanorand::WyRand::new().generate_range::<i64>(lower, upper), | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use rand; | ||
|
||
fn main() { | ||
let rng = rand::WyRand::new(); | ||
let rand_int = rng.int(); | ||
println(`Random int: {rand_int}`); | ||
let rand_int_range = rng.int_range(-100, 100); | ||
println(`Random int between -100 and 100: {rand_int_range}`); | ||
|
||
let rng = rand::Pcg64::new(); | ||
let rand_int = rng.int(); | ||
println(`Random int: {rand_int}`); | ||
let rand_int_range = rng.int_range(-100, 100); | ||
println(`Random int between -100 and 100: {rand_int_range}`); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FTR, these can simply return
i64
. Conversion is handled by Rune (for any type implementing theToValue
trait). Returning aValue
doesn't hurt though!