I’m creating a function where returns multiple of 10?
public static int NextInt(this Random rnd, int min = 0, int max = 1)
{
if (rnd == null) throw new ArgumentNullException("rnd");
if (min >= max) throw new InvalidOperationException();
var delta = max - min;
return min + (int)(rnd.NextDouble() * delta + min);
}
public static int MultipleOf10(this Random rnd, int minZeros = 1,
int maxZeros = 10)
{
if (rnd == null) throw new ArgumentNullException("rnd");
int pow = NextInt(rnd, minZeros, maxZeros);
return (int)Math.Pow(10, pow);
}
I’ve got doubt about the these two functions. The first one must be similar like Next() and the another one must return multiples of ten.
Can you point me where I fix it? Because I’m almost sure of that.
I think the last line of
NextInt()should beOr:
Or even better:
Also, you should keep in mind that the upper bounds of
Randommethods are exclusive. So, ifrnd.NextInt(i, j)should return numbers betweeniandjinclusive, you probably want to change the computation ofdeltato:or if you used the last option above, change it to: