2929
3030using System ;
3131using System . Collections . Generic ;
32+ using System . Numerics ;
33+ using System . Linq ;
3234
3335namespace 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