Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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

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

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.

Thanks

cheers

```
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));
}
```

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/>" );
}
}
```

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.