C#
[C#] Random Decimal
EdwardYang
2022. 8. 25. 14:51
반응형
public static class RandomDecimal
{
public static int NextInt32(this System.Random rng)
{
int firstBits = rng.Next(0, 1 << 4) << 28;
int lastBits = rng.Next(0, 1 << 28);
return firstBits | lastBits;
}
public static decimal NextDecimalSample(this System.Random random)
{
var sample = 1m;
while (sample >= 1)
{
var a = NextInt32(random);
var b = NextInt32(random);
var c = random.Next(542101087);
sample = new System.Decimal(a, b, c, false, 28);
}
return sample;
}
public static decimal NextDecimal(this System.Random random)
{
return NextDecimal(random, decimal.MaxValue);
}
public static decimal NextDecimal(this System.Random random, decimal maxValue)
{
return NextDecimal(random, decimal.Zero, maxValue);
}
public static decimal NextDecimal(this System.Random random, decimal minValue, decimal maxValue)
{
var nextDecimalSample = NextDecimalSample(random);
return maxValue * nextDecimalSample + minValue * (1 - nextDecimalSample);
}
}
반응형