From 3318cb2beddef50ddacc8e03d8375e449a946308 Mon Sep 17 00:00:00 2001 From: Vaibhav Sharma <48472541+GhostVaibhav@users.noreply.github.com> Date: Fri, 28 Jun 2024 19:39:37 +0530 Subject: [PATCH 1/4] add: added a randi() function for generating a random integer value --- src/Mages.Core/Runtime/Functions/SimpleRandom.cs | 6 ++++++ src/Mages.Core/Runtime/Functions/StandardFunctions.cs | 6 ++++++ src/Mages.Core/Runtime/Global.cs | 1 + 3 files changed, 13 insertions(+) diff --git a/src/Mages.Core/Runtime/Functions/SimpleRandom.cs b/src/Mages.Core/Runtime/Functions/SimpleRandom.cs index 260ca47..1f06cec 100644 --- a/src/Mages.Core/Runtime/Functions/SimpleRandom.cs +++ b/src/Mages.Core/Runtime/Functions/SimpleRandom.cs @@ -27,6 +27,12 @@ public static Double GetNumber() return _random.NextDouble(); } + public static Int32 GetInteger(Int32 maximum) + { + EnsureRandom(); + return (Int32)Math.Round(_random.NextDouble() * maximum); + } + private static Double[,] CreateMatrix(Int32 rows, Int32 cols) { var matrix = new Double[rows, cols]; diff --git a/src/Mages.Core/Runtime/Functions/StandardFunctions.cs b/src/Mages.Core/Runtime/Functions/StandardFunctions.cs index 6889b5f..781c5da 100644 --- a/src/Mages.Core/Runtime/Functions/StandardFunctions.cs +++ b/src/Mages.Core/Runtime/Functions/StandardFunctions.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Numerics; /// @@ -686,6 +687,11 @@ public static class StandardFunctions (args.Length > 0 ? If.Is(args, SimpleRandom.CreateVector) : null) ?? SimpleRandom.GetNumber()); + /// + /// Contains the random integer function. + /// + public static readonly Function Randi = new(args => SimpleRandom.GetInteger((int)args[0])); + /// /// Contains the throw function. /// diff --git a/src/Mages.Core/Runtime/Global.cs b/src/Mages.Core/Runtime/Global.cs index f31b9a9..a0feeae 100644 --- a/src/Mages.Core/Runtime/Global.cs +++ b/src/Mages.Core/Runtime/Global.cs @@ -41,6 +41,7 @@ static class Global { "gamma", StandardFunctions.Gamma }, { "sqrt", StandardFunctions.Sqrt }, { "rand", StandardFunctions.Rand }, + { "randi", StandardFunctions.Randi }, { "sin", StandardFunctions.Sin }, { "cos", StandardFunctions.Cos }, { "tan", StandardFunctions.Tan }, From ca0e442ac6b0629a654e840e4d68c419315dd658 Mon Sep 17 00:00:00 2001 From: Vaibhav Sharma <48472541+GhostVaibhav@users.noreply.github.com> Date: Fri, 28 Jun 2024 19:44:52 +0530 Subject: [PATCH 2/4] del: deleted an unused header --- src/Mages.Core/Runtime/Functions/StandardFunctions.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Mages.Core/Runtime/Functions/StandardFunctions.cs b/src/Mages.Core/Runtime/Functions/StandardFunctions.cs index 781c5da..7961a55 100644 --- a/src/Mages.Core/Runtime/Functions/StandardFunctions.cs +++ b/src/Mages.Core/Runtime/Functions/StandardFunctions.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Net; using System.Numerics; /// From f61bf19c365035246db383cdd62d58087ae455f6 Mon Sep 17 00:00:00 2001 From: Vaibhav Sharma <48472541+GhostVaibhav@users.noreply.github.com> Date: Fri, 28 Jun 2024 19:47:59 +0530 Subject: [PATCH 3/4] refactor: refactored the documentation to include the new function --- doc/functions.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/doc/functions.md b/doc/functions.md index dcc75a2..bc57006 100644 --- a/doc/functions.md +++ b/doc/functions.md @@ -261,6 +261,14 @@ Works without any arguments. x = rand() // any number between 0 and 1 ``` +### Generate Single Random Integer + +Works with one argument. + +``` +x = randi(5) // any integer between 0 and 5 +``` + ### Generate Random Vector Works with one argument. From 084c78a7e50e175fa04baf0dc2abfdc09c38f42c Mon Sep 17 00:00:00 2001 From: Vaibhav Sharma <48472541+GhostVaibhav@users.noreply.github.com> Date: Fri, 28 Jun 2024 20:41:41 +0530 Subject: [PATCH 4/4] refactor: added currying and checks for arguments --- src/Mages.Core/Runtime/Functions/StandardFunctions.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Mages.Core/Runtime/Functions/StandardFunctions.cs b/src/Mages.Core/Runtime/Functions/StandardFunctions.cs index 7961a55..cc102fb 100644 --- a/src/Mages.Core/Runtime/Functions/StandardFunctions.cs +++ b/src/Mages.Core/Runtime/Functions/StandardFunctions.cs @@ -689,7 +689,9 @@ public static class StandardFunctions /// /// Contains the random integer function. /// - public static readonly Function Randi = new(args => SimpleRandom.GetInteger((int)args[0])); + public static readonly Function Randi = new(args => Curry.MinOne(Randi, args) ?? + If.Is(args, x => SimpleRandom.GetInteger((int)x)) ?? + 0); /// /// Contains the throw function.