# Thread: probability branching

1. ## probability branching

Code:
```     public static Random random = new Random();     public static int num = random.Next(100, 1000);

public static void linked_group()
{
if (num == 257)
{ group2(); }
else { group1(); }

return;
}```
Will this work? 25.7% of the time I need to branch to group 2

2. That depends on the period of the Random() pseudo random number generator so most likely no it will not. You will need some code that tracks the amount of attempts and forces success even if the random value is incorrect IF the ratio of success to failure is 25.7%.

For instance if you wanted code to take a certain branch one of out of every ten times you would need some variable to track the count of how many times the conditional has been evaluated and force the code to take a certain branch if the ratio was correct. You cannot rely on a pseudo-random number generator because you do not know for sure that 1 out of every 10 times it will pick your number even if the range is 1 to 10. It could pick the wrong number far more than 1 in 10 times and probably will.

3. Well, you're looking for one number out of 899 with that if statement along with the random number generator.

4. Why not just if(random.NextDouble() <= 0.257) group2(); else group1();?

5. Thanks for the suggestions.
This is how I now have it coded.

Code:
```  // linked section

{

// Brown

if (sb <= jw.Brown)
{ jw.siredata[x, 2] = sire[4]; }
else { jw.siredata[x, 2] = sire[5]; }
if (sb == jw.Brown) { sb = 1; } // resets Brown counter
}

{
// Colour
if (sc <= jw.Colour)
{ jw.siredata[x,3] = sire[7]; }
if (sc == jw.Colour) { sc = 1; } // resets Colour counter
}

{ // check if valid pair

{
paired [0] = jw.siredata[x,2];
paired [1] = jw.siredata[x,3];}

// Convert array paired to string

StringBuilder builder = new StringBuilder();
foreach (string value in paired)
{
builder.Append(value);
builer.Append('.');
}

{

if (random.NextDouble() <= 0.257)
{ crossed = true;}
else
{
Random random = new Random();
int num = random.Next(0,1);

}
}

} // end linked section```
Will this work?

6. This seems odd...
Code:
```                      if (random.NextDouble() <= 0.257)
{ crossed = true;}
else
{
Random random = new Random();```

Popular pages Recent additions