Thread: probability branching

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    20

    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. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.
    Last edited by VirtualAce; 11-16-2011 at 07:12 PM.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    Well, you're looking for one number out of 899 with that if statement along with the random number generator.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Why not just if(random.NextDouble() <= 0.257) group2(); else group1();?
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    20
    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?

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    This seems odd...
    Code:
                          if (random.NextDouble() <= 0.257)
                          { crossed = true;}
                          else
                          {
                               Random random = new Random();
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Probability
    By Perspective in forum General Discussions
    Replies: 3
    Last Post: 10-27-2010, 12:32 PM
  2. Using char arrays in branching statements
    By hmanners in forum C Programming
    Replies: 18
    Last Post: 06-08-2009, 04:03 AM
  3. probability
    By sycamorex in forum Windows Programming
    Replies: 0
    Last Post: 11-25-2006, 06:10 AM
  4. Branching?
    By Brian in forum C Programming
    Replies: 1
    Last Post: 01-20-2002, 11:06 AM
  5. probability
    By billytsc in forum C++ Programming
    Replies: 0
    Last Post: 10-18-2001, 02:22 PM