Skip to content

Commit e7bcab1

Browse files
committed
Revert "Revert "RandomExtensions.NextBigIntegerSequence""
This reverts commit b9104ae.
1 parent ca8f1f2 commit e7bcab1

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

src/Numerics.Tests/Random/RandomExtensionTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
using MathNet.Numerics.Random;
3131
using NUnit.Framework;
32+
using System.Numerics;
33+
using System.Collections.Generic;
3234

3335
namespace MathNet.Numerics.UnitTests.Random
3436
{
@@ -48,6 +50,22 @@ public void CanSampleInt64()
4850
rnd.NextInt64();
4951
}
5052

53+
/// <summary>
54+
/// Can sample BigInteger
55+
/// </summary>
56+
[Test]
57+
public void CanSampleBigInteger()
58+
{
59+
var rnd = new System.Random(0);
60+
IEnumerator<BigInteger> Sequence = rnd.NextBigIntegerSequence((BigInteger)long.MinValue * 3, (BigInteger)long.MaxValue * 3).GetEnumerator();
61+
Sequence.MoveNext();
62+
System.Console.WriteLine(Sequence.Current);
63+
Sequence.MoveNext();
64+
System.Console.WriteLine(Sequence.Current);
65+
Sequence.MoveNext();
66+
System.Console.WriteLine(Sequence.Current);
67+
}
68+
5169
/// <summary>
5270
/// Can sample full range int32.
5371
/// </summary>

src/Numerics/Random/RandomExtensions.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
using System;
3131
using System.Collections.Generic;
32+
using System.Numerics;
33+
using System.Linq;
3234

3335
namespace MathNet.Numerics.Random
3436
{
@@ -167,7 +169,7 @@ public static void NextInt32s(this System.Random rnd, int[] values, int minInclu
167169
}
168170

169171
/// <summary>
170-
/// Returns an infinite sequence of uniform random numbers greater than or equal to 0.0 and less than 1.0.
172+
/// Returns an infinite sequence of uniform random 32-bit signed integers within the specified range.
171173
/// </summary>
172174
/// <remarks>
173175
/// This extension is thread-safe if and only if called on an random number
@@ -191,6 +193,34 @@ static IEnumerable<int> NextInt32SequenceEnumerable(System.Random rnd, int minIn
191193
}
192194
}
193195

196+
/// <summary>
197+
/// Returns an infinite sequence of uniform random <see cref="System.Numerics.BigInteger"/> within the specified range.
198+
/// </summary>
199+
/// <remarks>
200+
/// This extension is thread-safe if and only if called on an random number
201+
/// generator provided by Math.NET Numerics or derived from the RandomSource class.
202+
/// </remarks>
203+
public static IEnumerable<BigInteger> NextBigIntegerSequence(this System.Random rnd, BigInteger minInclusive, BigInteger maxExclusive)
204+
{
205+
BigInteger AbsoluteRange = maxExclusive - minInclusive;
206+
int NumBytes = (int)Math.Ceiling(BigInteger.Log(AbsoluteRange, byte.MaxValue) * 2);
207+
byte[] ByteSequence = Enumerable.Repeat(byte.MaxValue, NumBytes + 1).ToArray();
208+
ByteSequence[NumBytes] = 0;
209+
BigInteger RandomNumber = new BigInteger(ByteSequence);
210+
BigInteger ValidRange = RandomNumber - RandomNumber % AbsoluteRange;
211+
while(true)
212+
{
213+
do
214+
{
215+
rnd.NextBytes(ByteSequence);
216+
ByteSequence[NumBytes] = 0;
217+
RandomNumber = new BigInteger(ByteSequence);
218+
}
219+
while (RandomNumber >= ValidRange);
220+
yield return RandomNumber % AbsoluteRange + minInclusive;
221+
}
222+
}
223+
194224
/// <summary>
195225
/// Returns a nonnegative random number less than <see cref="Int64.MaxValue"/>.
196226
/// </summary>

0 commit comments

Comments
 (0)