المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : how can I run code dependent on a probability using a random object?



C# Programming
04-19-2013, 12:40 AM
I have created a genetic algorithmic program, but when it comes to doing the crossover or mutation probability I am having to do this by hard coding the function. So My default crossover is a 70% chance of the method body running depending on the result of a random generation, I am using a random number between 1 and 10, storing 7 numbers in an array and checking if they are 8, 9 or 10 is selected then the method body does not run.

Here is my code:

for (int i = 0; i < 7; i++) { pc[i] = random3.Next(1, 11); } int n = random3.Next(0, 7); if ((pc[n] != 8) && (pc[n] != 9) && (pc[n] != 10)) { label58.Text = "YES!"; } else { label58.Text = "NO!"; }
I would like a better way to do this so I can choose a percentage probability by asking the user to enter a percentage. In real life the mutation probability is around 0.001, when it comes to this, I am having issues figuring out the best way to do this.

I would greatly appreciate your advice.