I want to trigger an event with a probability of 25% based on a random number generated between 1 and 100 using:
int rand = random.Next(1,100);
Will the following achieve this?
if (rand<=25)
{
// Some event...
}
I thought I would use a number between 1 and 100 so I can tweak the probabilities later on - e.g. adjust to 23% by using
if (rand<=23) {...}
The second argument of Next(int, int)
is the exclusive upper bound of the desired range of results. You should therefore use this:
if (random.Next(0, 100) < 25)
or, if you must use 1-based logic,
if (random.Next(1, 101) <= 25)