new function: random()

random() returns a random number between 0 and 1
This commit is contained in:
Ophir LOJKINE 2022-03-30 14:39:26 +02:00
parent c514410323
commit bce31e24ae
5 changed files with 31 additions and 1 deletions

View File

@ -26,6 +26,7 @@ path = "src/lib.rs"
regex = { version = "1.5.5", optional = true} regex = { version = "1.5.5", optional = true}
serde = { version = "1.0.133", optional = true} serde = { version = "1.0.133", optional = true}
serde_derive = { version = "1.0.133", optional = true} serde_derive = { version = "1.0.133", optional = true}
rand = { version = "0.8", optional = true}
[features] [features]
serde_support = ["serde", "serde_derive"] serde_support = ["serde", "serde_derive"]
@ -33,5 +34,5 @@ regex_support = ["regex"]
[dev-dependencies] [dev-dependencies]
ron = "0.7.0" ron = "0.7.0"
rand = "0.8.4" rand = "0.8.5"
rand_pcg = "0.3.1" rand_pcg = "0.3.1"

View File

@ -380,6 +380,7 @@ This crate offers a set of builtin functions.
| `bitnot` | 1 | Int | Computes the bitwise not of the given integer | | `bitnot` | 1 | Int | Computes the bitwise not of the given integer |
| `shl` | 2 | Int | Computes the given integer bitwise shifted left by the other given integer | | `shl` | 2 | Int | Computes the given integer bitwise shifted left by the other given integer |
| `shr` | 2 | Int | Computes the given integer bitwise shifted right by the other given integer | | `shr` | 2 | Int | Computes the given integer bitwise shifted right by the other given integer |
| `random` | 0 | Empty | Return a random float between 0 and 1. Requires the `rand` feature flag. |
The `min` and `max` functions can deal with a mixture of integer and floating point arguments. The `min` and `max` functions can deal with a mixture of integer and floating point arguments.
If the maximum or minimum is an integer, then an integer is returned. If the maximum or minimum is an integer, then an integer is returned.

View File

@ -185,6 +185,11 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
"str::from" => Some(Function::new(|argument| { "str::from" => Some(Function::new(|argument| {
Ok(Value::String(argument.to_string())) Ok(Value::String(argument.to_string()))
})), })),
#[cfg(feature = "rand")]
"random" => Some(Function::new(|argument| {
argument.as_empty()?;
Ok(Value::Float(rand::random()))
})),
// Bitwise operators // Bitwise operators
"bitand" => int_function!(bitand, 2), "bitand" => int_function!(bitand, 2),
"bitor" => int_function!(bitor, 2), "bitor" => int_function!(bitor, 2),

View File

@ -363,6 +363,7 @@
//! | `bitnot` | 1 | Int | Computes the bitwise not of the given integer | //! | `bitnot` | 1 | Int | Computes the bitwise not of the given integer |
//! | `shl` | 2 | Int | Computes the given integer bitwise shifted left by the other given integer | //! | `shl` | 2 | Int | Computes the given integer bitwise shifted left by the other given integer |
//! | `shr` | 2 | Int | Computes the given integer bitwise shifted right by the other given integer | //! | `shr` | 2 | Int | Computes the given integer bitwise shifted right by the other given integer |
//! | `random` | 0 | Empty | Return a random float between 0 and 1. Requires the `rand` feature flag. |
//! //!
//! The `min` and `max` functions can deal with a mixture of integer and floating point arguments. //! The `min` and `max` functions can deal with a mixture of integer and floating point arguments.
//! If the maximum or minimum is an integer, then an integer is returned. //! If the maximum or minimum is an integer, then an integer is returned.

22
tests/rand.rs Normal file
View File

@ -0,0 +1,22 @@
#![cfg(feature = "rand")]
use evalexpr::*;
fn assert_expr(expr: &str) {
assert_eq!(eval(expr), Ok(Value::Boolean(true)))
}
#[test]
fn test_random() {
for _ in 0..100 {
assert_expr("random() != random()");
assert_expr("0 <= random()");
assert_expr("random() <= 1");
}
}
#[test]
fn test_random_errors() {
assert!(eval("random(9)").is_err());
assert!(eval("random(\"a\", \"b\")").is_err());
}