Added cancellation token

This commit is contained in:
Thorsten Sommer 2020-09-30 19:48:35 +02:00
parent 01e6355e4f
commit a00f5f6e9e

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace FastRng.Double namespace FastRng.Double
@ -29,11 +30,11 @@ namespace FastRng.Double
} }
} }
public async ValueTask<double> NextNumber() public async ValueTask<double> NextNumber(CancellationToken token = default)
{ {
while (true) while (!token.IsCancellationRequested)
{ {
var nextNumber = await this.rng.GetUniform(); var nextNumber = await this.rng.GetUniform(token);
var nextBucket = (int)Math.Floor(nextNumber * this.sampleSize); var nextBucket = (int)Math.Floor(nextNumber * this.sampleSize);
this.samples[nextBucket] += this.probabilities[nextBucket]; this.samples[nextBucket] += this.probabilities[nextBucket];
@ -43,6 +44,8 @@ namespace FastRng.Double
return nextNumber; return nextNumber;
} }
} }
return double.NaN;
} }
} }
} }