13

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) {...} 
4
  • 1
    Have you tried it? It should work fine. Just be careful you don't recreate many Random instances in a short time window, or they will return the same "random" result. Commented Apr 9, 2012 at 21:12
  • I don't want to use 1 to 4 as I want to tweak later on if need be, but yes, 1,4 would be the same. Thanks Scott, I missed that. Commented Apr 9, 2012 at 21:13
  • That sounds about right. Commented Apr 9, 2012 at 21:14
  • 1
    Yeah, I made that mistake a week or so ago - I create the random object as an instance variable to get around this. Commented Apr 9, 2012 at 21:14

3 Answers 3

14

The biggest error you are making is it should be random.Next(0,100) as the documentation states

minValue: The inclusive lower bound of the random number returned.

maxValue: The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.

Emphisis mine, exclusive means it does not include number you passed in, so my code generates the range 0-99 and your code generates the range 1-99.

So change your code to

int rand = random.Next(0,100) if (rand < 25) //25% { // Some event... } //other code if (rand < 23) //23% { // Some event... } 

The change from <= to < is because you are now using the exclusive upper bounds range

Sign up to request clarification or add additional context in comments.

Comments

10

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) 

5 Comments

This is interesting, as I was thinking I could try and trigger another 25% chance event using the same random number via: 'if ((rand>25)&&(rand<=50)) {...}. But I guess just having a series of if statements as you suggest would work just as well?
@CdrTomalak that won't work at all. If your first event was invoked, the probability of invoking the next event is zero. If the first event was not invoked, the probability of invoking the next event is 1/3. If you reuse the random number, the events will no longer be independently random; as there will be no way that both events could occur.
@CdrTomalak if, on the other hand, you want to select one of four events at random, you're on the right track; you need to use if (rand >= 25 && rand < 50) for zero-based numbers, because the lower bound is inclusive, and the upper bound is inclusive. For 1-based numbers, your expression would be correct.
I was not thinking of testing for 'if ((rand>25)&&(rand<=50))' inside the first if block, but rather immediately afterwards. In this case if the first event was not invoked the second might be. This is slightly confusing but I think you might be right, and I should generate a new random number before each test.
@CdrTomalak When the first event is not invoked, do you want the second event to have a 1/4 chance of happening? If so, generate a new number. There will be a 47.75% chance of one event happening. Otherwise, if you want a 1/3 chance, you can reuse the previous number. There will be a 50% chance of one event happening.
8

You can also use this code (usually for percentage calculations double between 0 and 1 is used):

double rand = random.NextDouble(); if(rand < .25) { ... 

7 Comments

That should be if (rand < .25); the upper bound is exclusive: msdn.microsoft.com/en-us/library/system.random.nextdouble.aspx
Thank you. I edited my answer if you don't mind. But for double it doesn't make much difference. (Difference is only when numbers like 0.249999999... and 0.25 come into play, which doesn't spoil picture of 1/4 probability)
Yes, the difference is very small (the precise impact depends on how many discrete values the NextDouble function could return, of course). I suppose if the algorithm were so sensitive that this made a difference, then it should use true random rather than pseudo-random numbers in the first place.
@phoog. The range of numbers is 0.0 <= random < 1.0, therefore < 0.25 is correct! You have the four quarters [0.0 <= x < 0.25], [0.25 <= x < 0.5], [0.5 <= x < 0.75] and [0.75 <= x < 1.0], which are all of exactly the same size.
Now, we are all more smart :-)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.