
BUG OF THE MONTH | Expression is always true
public virtual DateTime NextDate(....)
{
....
// both are valid dates, so chose one randomly
if ( IsWithinRange(nextDayOfWeek, minDateTime, maxDateTime)
&& IsWithinRange(previousDayOfWeek, minDateTime, maxDateTime))
{
return _random.Next(0, 1) == 0
? previousDayOfWeek
: nextDayOfWeek;
}
....
}
V3022 Expression ‘_random.Next(0, 1) == 0’ is always true. RandomValueGenerator.cs 142
The bottom line was that either one or the other value was selected with a 50% probability. However, in this case, the Next method will always return 0.
This happens because the second argument is not included in the range of values. That is, the value that the method can return will be in the range [0,1). Let’s fix that:
public virtual DateTime NextDate(....)
{
....
// both are valid dates, so chose one randomly
if ( IsWithinRange(nextDayOfWeek, minDateTime, maxDateTime)
&& IsWithinRange(previousDayOfWeek, minDateTime, maxDateTime))
{
return _random.Next(0, 2) == 0
? previousDayOfWeek
: nextDayOfWeek;
}
....
}
Please click here to see more bugs from this project.