Skip to content

Commit 0381d18

Browse files
authored
Merge pull request #124 from GhostVaibhav/ghostvaibhav/123
Added a randi() function to generate random integers
2 parents dbb5abb + 084c78a commit 0381d18

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

doc/functions.md

+8
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,14 @@ Works without any arguments.
261261
x = rand() // any number between 0 and 1
262262
```
263263

264+
### Generate Single Random Integer
265+
266+
Works with one argument.
267+
268+
```
269+
x = randi(5) // any integer between 0 and 5
270+
```
271+
264272
### Generate Random Vector
265273

266274
Works with one argument.

src/Mages.Core/Runtime/Functions/SimpleRandom.cs

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public static Double GetNumber()
2727
return _random.NextDouble();
2828
}
2929

30+
public static Int32 GetInteger(Int32 maximum)
31+
{
32+
EnsureRandom();
33+
return (Int32)Math.Round(_random.NextDouble() * maximum);
34+
}
35+
3036
private static Double[,] CreateMatrix(Int32 rows, Int32 cols)
3137
{
3238
var matrix = new Double[rows, cols];

src/Mages.Core/Runtime/Functions/StandardFunctions.cs

+7
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,13 @@ public static class StandardFunctions
686686
(args.Length > 0 ? If.Is<Double>(args, SimpleRandom.CreateVector) : null) ??
687687
SimpleRandom.GetNumber());
688688

689+
/// <summary>
690+
/// Contains the random integer function.
691+
/// </summary>
692+
public static readonly Function Randi = new(args => Curry.MinOne(Randi, args) ??
693+
If.Is<Double>(args, x => SimpleRandom.GetInteger((int)x)) ??
694+
0);
695+
689696
/// <summary>
690697
/// Contains the throw function.
691698
/// </summary>

src/Mages.Core/Runtime/Global.cs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static class Global
4141
{ "gamma", StandardFunctions.Gamma },
4242
{ "sqrt", StandardFunctions.Sqrt },
4343
{ "rand", StandardFunctions.Rand },
44+
{ "randi", StandardFunctions.Randi },
4445
{ "sin", StandardFunctions.Sin },
4546
{ "cos", StandardFunctions.Cos },
4647
{ "tan", StandardFunctions.Tan },

0 commit comments

Comments
 (0)