From bce31e24aea09367c11547b534a963cdf4836e69 Mon Sep 17 00:00:00 2001 From: Ophir LOJKINE Date: Wed, 30 Mar 2022 14:39:26 +0200 Subject: [PATCH 1/4] new function: random() random() returns a random number between 0 and 1 --- Cargo.toml | 3 ++- README.md | 1 + src/function/builtin.rs | 5 +++++ src/lib.rs | 1 + tests/rand.rs | 22 ++++++++++++++++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/rand.rs diff --git a/Cargo.toml b/Cargo.toml index 7868b66..96b68e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ path = "src/lib.rs" regex = { version = "1.5.5", optional = true} serde = { version = "1.0.133", optional = true} serde_derive = { version = "1.0.133", optional = true} +rand = { version = "0.8", optional = true} [features] serde_support = ["serde", "serde_derive"] @@ -33,5 +34,5 @@ regex_support = ["regex"] [dev-dependencies] ron = "0.7.0" -rand = "0.8.4" +rand = "0.8.5" rand_pcg = "0.3.1" diff --git a/README.md b/README.md index f493b02..d2fba5d 100644 --- a/README.md +++ b/README.md @@ -380,6 +380,7 @@ This crate offers a set of builtin functions. | `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 | | `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. If the maximum or minimum is an integer, then an integer is returned. diff --git a/src/function/builtin.rs b/src/function/builtin.rs index 7194da1..0b5f1fa 100644 --- a/src/function/builtin.rs +++ b/src/function/builtin.rs @@ -185,6 +185,11 @@ pub fn builtin_function(identifier: &str) -> Option { "str::from" => Some(Function::new(|argument| { Ok(Value::String(argument.to_string())) })), + #[cfg(feature = "rand")] + "random" => Some(Function::new(|argument| { + argument.as_empty()?; + Ok(Value::Float(rand::random())) + })), // Bitwise operators "bitand" => int_function!(bitand, 2), "bitor" => int_function!(bitor, 2), diff --git a/src/lib.rs b/src/lib.rs index f136143..9bf09d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -363,6 +363,7 @@ //! | `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 | //! | `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. //! If the maximum or minimum is an integer, then an integer is returned. diff --git a/tests/rand.rs b/tests/rand.rs new file mode 100644 index 0000000..2e7ceb7 --- /dev/null +++ b/tests/rand.rs @@ -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()); +} \ No newline at end of file From 7c90768afb87622f7f21a4d44f5682fc604df34c Mon Sep 17 00:00:00 2001 From: Ophir LOJKINE Date: Wed, 30 Mar 2022 14:46:23 +0200 Subject: [PATCH 2/4] add newline at the end of the file to make cargo fmt happy --- tests/rand.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/rand.rs b/tests/rand.rs index 2e7ceb7..cec07c5 100644 --- a/tests/rand.rs +++ b/tests/rand.rs @@ -19,4 +19,4 @@ fn test_random() { fn test_random_errors() { assert!(eval("random(9)").is_err()); assert!(eval("random(\"a\", \"b\")").is_err()); -} \ No newline at end of file +} From 7227d02a9c1ef296613f7b55868c76531fa8d62b Mon Sep 17 00:00:00 2001 From: lovasoa Date: Wed, 20 Apr 2022 20:53:08 +0200 Subject: [PATCH 3/4] Add a comment for the random() != random() test --- tests/rand.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/rand.rs b/tests/rand.rs index cec07c5..4f4914b 100644 --- a/tests/rand.rs +++ b/tests/rand.rs @@ -9,6 +9,7 @@ fn assert_expr(expr: &str) { #[test] fn test_random() { for _ in 0..100 { + // This has a probability of 1e-20 of failing assert_expr("random() != random()"); assert_expr("0 <= random()"); assert_expr("random() <= 1"); From 3358ba3c4a6e3ece1a6a86680d71aeb85735ec52 Mon Sep 17 00:00:00 2001 From: lovasoa Date: Wed, 20 Apr 2022 20:55:17 +0200 Subject: [PATCH 4/4] Refuse versions of rand before 0.8.5 https://github.com/ISibboI/evalexpr/pull/102#discussion_r845129883 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 96b68e3..e7840db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ path = "src/lib.rs" regex = { version = "1.5.5", optional = true} serde = { version = "1.0.133", optional = true} serde_derive = { version = "1.0.133", optional = true} -rand = { version = "0.8", optional = true} +rand = { version = "0.8.5", optional = true} [features] serde_support = ["serde", "serde_derive"]