15

I have this code snippet:

Random rand = new Random();
int chance = rand.Next(1, 101);

if (chance <= 25) // probability of 25%
{
    Console.WriteLine("You win");
}
else
{
    Console.WriteLine("You lose");
}

My question is, does it really calculate a 25% probability for winning here? Is the chance of winning for the player here is really 25%?

Edit:

I just wrote this:

        double total = 0;
        double prob = 0;
        Random rnd = new Random();
        for (int i = 0; i < 100; i++)
        {
            double chance = rnd.Next(1, 101);
            if (chance <= 25) prob++;
            total++;
        }
        Console.WriteLine(prob / total);
        Console.ReadKey();

And it's highly inaccurate. It goes from about 0.15 to 0.3.

But when I do more checks (change from (i < 100) to (i < 10000)) it's much more accurate.

Why is this? Why aren't 100 checks enough for it to be accurate?

BlueRay101
  • 1,447
  • 2
  • 18
  • 29
  • Yes, providing rand is giving a normal distribution of random values. – Vlad Dec 06 '13 at 17:55
  • 2
    @Vlad I think you mean _uniform_ distribution. Normal distribution is Gaussian. – clcto Dec 06 '13 at 17:58
  • You're right, I meant one thing and wrote another. Thanks. – Vlad Dec 06 '13 at 18:01
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Dec 06 '13 at 18:12
  • 3
    if you rolled a die 6 times would you call it "highly inaccurate" if you got 2 or 3 of one number instead of 1 of each? – AwokeKnowing Dec 06 '13 at 19:22
  • 1
    The variable total follows a binomial distribution with p=0.25. The standard deviation of total for 100 trials is thus sqrt(25) = 5. Using a normal approximation you'd thus only expect ~66% of tests to be in the interval 20 <= total <= 30, which means your calculated p is only between .2 and .3 66% of the time. If you increase to 1000 the same 66% interval is .235 <= total <= .265. For 10000 it's .245 <= total <= .255 – Ben Allison Dec 10 '13 at 12:15
  • Possible duplicate of [Random number generator only generating one random number](https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) – Jim G. Dec 26 '18 at 13:58

3 Answers3

16

This is very easy to check for yourself:

Random rand = new Random(); 
int yes = 0;
const int iterations = 10000000;
for (int i = 0; i < iterations; i++)
{
   if (rand.Next(1, 101) <= 25)
   {
       yes++;
   }
}
Console.WriteLine((float)yes/iterations);

the result:

0.2497914

The conslusion: Yes, yes it is.


Edit: Just for fun, the LINQy version:

Random rand = new Random(); 
const int iterations = 10000000;
int sum = Enumerable.Range(1, iterations)
                    .Count(i => rand.Next(1, 101) <= 25);
Console.WriteLine(sum / (float)iterations);
Rotem
  • 21,452
  • 6
  • 62
  • 109
  • Thank you, but I've updated the post with a code that does a similar check. The problem is that if it checks 100 times, the result can be anywhere from 0.15 to 0.3, but when I check 10000 times, it much more accurate (in the code I wrote). Why is this so? Why aren't 100 checks enough? – BlueRay101 Dec 06 '13 at 18:54
  • 12
    If you flip a coin twice and get the same result is the coin broken? – Rotem Dec 07 '13 at 14:16
  • @Rotem I think you need a sufficient sample size. I don't think you can use 25 samples to determine if 25% probability is working correctly, for instance. – The Muffin Man Aug 28 '21 at 00:13
  • There are not 25 samples, there are 10 million. – Rotem Aug 28 '21 at 00:15
1

yes, that should work fine. just as >=75 would work too. if you just want 25%, you can go ahead and just do a random number between 1 and 4.

but please note, a 25% chance does NOT mean that out of 100 tries, he will win 25 times. It just means each time he has a 25% chance of winning. it's theoretically possible for him to win every single time. (but that will not happen, especially with a pseudo-random generator).

Internally the random number will be between 0 and 1 so it's just as random to use 4 as 1000, as far as that goes. add the parameter just projects it to the range you want.

AwokeKnowing
  • 7,728
  • 9
  • 36
  • 47
  • I know, but sometimes I can't just use fractions (for example in percentages like 65% or 55%), so I'm planning to use this for other percentages too. Thank you very much for your help. – BlueRay101 Dec 06 '13 at 18:00
1

For most cases, I would say yes. However, you have to remember that most randomization algorithms use a pseudo-random generator, and so to some extent, you're at the mercy of the idiosyncrasies of that particular generator. I do agree with @AwokeKnowing that you can you also just do a random number between 1 and 4 and get the same result. I assume that the .Net randomization algorithm should suffice for most cases. For more info see:

http://en.wikipedia.org/wiki/Pseudorandom_number_generator

Gerald T
  • 25
  • 5