# probability branching

• 11-16-2011
Dale
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
• 11-16-2011
VirtualAce
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.
• 11-17-2011
DanFraser
Well, you're looking for one number out of 899 with that if statement along with the random number generator.
• 11-17-2011
itsme86
Why not just if(random.NextDouble() <= 0.257) group2(); else group1();?
• 11-17-2011
Dale
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 ((builder) == (unlinked1 || unlinked2))                                           if (random.NextDouble() <= 0.257)                         { crossed = true;}                         else                         {                             Random random = new Random();                             int num = random.Next(0,1);                                                       jw.siredata[x,3] = linkedpairs[num,0];                             jw.siredata[x,4] = linkedpairs[num,1];                           }                     }                                     } // end linked section```
Will this work?
• 11-18-2011
itsme86
This seems odd...
Code:

```                      if (random.NextDouble() <= 0.257)                       { crossed = true;}                       else                       {                           Random random = new Random();```