Solved

Calculating Probability using C#

Posted on 2009-03-29
8
1,677 Views
Last Modified: 2013-12-17
Hello,

I'm wondering how to approach a problem of calculating probability in C#.
Lets say, we know that some task has a 5% probability of happening and there're say 100 identical tasks. So theoretically, 5 tasks should happen, but as those tasks are individual tasks, each task has a 5% probability of happening. So, how can I simulate this case using programming?

I presume that I can get 5 random number using Random class (Random rand = new Random(); rand.Next(1, 100)), and then get another 100 numbers and see if any number is in the set I generated first. But in this case, it's not truly random as generating first 5 numbers can use all "random potential" of these numbers and they will happened to be generated again.

Is there any accurate way of doing it?

Thanks
0
Comment
Question by:Yurich
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 18

Expert Comment

by:philipjonathan
ID: 24015358
How about this:
For each task, generate a random number between (1 to 100), and check if the number is > 95. Assuming that the random generator is uniformly distributed (which it claims so), then there is a 5% probability that the task gets > 95. Repeat this for every task.
0
 
LVL 21

Author Comment

by:Yurich
ID: 24015493
yes, I was initially thinking about doing something like that, but just checking if those numbers are from 1 to 5. But that seems to me not very random as for this particular run, all numbers can happen to be from 5 to 100. That's why I started to think about generating random 5 numbers first, rather than using any particular sets (1-5, 20-25, 96-100, etc).

Thanks
0
 
LVL 18

Accepted Solution

by:
philipjonathan earned 500 total points
ID: 24015560
Well, I'm not a probability expert myself. But since the random generator is uniformly distributed, so there would be equal chance of you getting 50 as you getting 99. In effect that gives a task a 5% probability of getting happening.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Author Comment

by:Yurich
ID: 24015631
Just wrote a quick piece of code to check this theory and in fact, greater the number of repetitions, more chances of evenly spreading of the results. I'll keep it open for a while in case someone has a different approach, but I will probably just do as I first intended (and as phillip suggested).

cheers
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 24015659
Hmm, seems like we think alike, I've just run my own test as well. The test code is as attached. Basically I run 10000 times of set of 100. For each run (100 numbers), I note down the number of yes and no. Granted that sometimes, there are > 5 yes, sometimes there are < 5 yes. But with 10000 times run, the result average out to about 5 (in my test it's 4.95xxx)
        static void Main(string[] args)
        {
            Random g = new Random();
            int avg = 0;
            for (int i = 0; i < 10000; i++)
            {
                int yes = 0;
                int no = 0;
                for (int j = 0; j < 100; j++)
                {
                    int x = g.Next(0, 100);
                    if (x >= 95)
                        yes++;
                    else
                        no++;
                }
                System.Diagnostics.Debug.WriteLine(string.Format("Yes {0} No {1}", yes, no));
                avg += yes;
            }
            System.Diagnostics.Debug.WriteLine(string.Format("Avg {0}", avg));
        }

Open in new window

0
 
LVL 21

Author Comment

by:Yurich
ID: 24015713
lol,

I've done a bit different thing, but for 100,000 runs, distribution is very even. The code is attached; each number in "groups" is a segment of 5 numbers (1-5, 6-10, etc.)
    protected void Page_Load(object sender, EventArgs e)
    {
		Random rand = new Random();
 
		int[] groups = new int[ 20 ];
		int number = 0;
 
		for( int i = 0; i < 100000; i++ )
		{
			number = rand.Next( 0, 100 );
			groups[ number / 5 ]++;
		}
 
		for( int i = 0; i < 20; i++ )
		{
			Response.Write( Math.Round( ( double )groups[ i ] / 100000, 2 ) + "<br/>" );
		}
 
    }

Open in new window

0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 24016218
Yup, I ran your code and saw that the distribution is very even, I even had to increase the number of decimal points just to ensure that I'm seeing the real thing. I think this is expected because the random generator is evenly distributed.
0
 
LVL 21

Author Closing Comment

by:Yurich
ID: 31564147
thanks for confirming my thoughts :)
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question