in a C# ConsoleApp, I made a method that will either return true or false, based on its possibility parameter that you give it, so if you give it 5, there’s a 50% that you’ll get true, same for false, give it 9, there’s a 90% chance that you’ll get true, 10% false.
Here it is:
(I was writing this as a tutorial, and I was explaining it in that
balls-ThatGotNumbers-inside-a-box kinda way to make it clear, hence the name of the variable BallNumber)
static public bool TrueOrFalse(int possiblity)
{
Random rand = new Random();
int BallNumber = rand.Next(0, 10); // BallNumber values are from the interval: [0, 10[ (0, 1, 2, ....9)
if (possiblity <= 0)
return false;
if (possiblity == 1)
return (BallNumber == 0); // any number you like.
if (possiblity == 2)
return (BallNumber == 0 || BallNumber == 1); // any two numbers, cuz there's a 20% chance that you might get a ball from the box that has either number n OR m.
if (possiblity == 3)
return (BallNumber == 0 || BallNumber == 1 || BallNumber == 2); // any three numbers ... etc you got the point.
if (possiblity == 4)
return (BallNumber == 0 || BallNumber == 1 || BallNumber == 2 || BallNumber == 3);
if (possiblity == 5)
return (BallNumber == 0 || BallNumber == 1 || BallNumber == 2 || BallNumber == 3 || BallNumber == 4);
if (possiblity == 6)
return (BallNumber == 0 || BallNumber == 1 || BallNumber == 2 || BallNumber == 3 || BallNumber == 4 || BallNumber == 5);
if (possiblity == 7)
return (BallNumber == 0 || BallNumber == 1 || BallNumber == 2 || BallNumber == 3 || BallNumber == 4 || BallNumber == 5 || BallNumber == 6);
if (possiblity == 8)
return (BallNumber == 0 || BallNumber == 1 || BallNumber == 2 || BallNumber == 3 || BallNumber == 4 || BallNumber == 5 || BallNumber == 6 || BallNumber == 7);
if (possiblity == 9)
return (BallNumber == 0 || BallNumber == 1 || BallNumber == 2 || BallNumber == 3 || BallNumber == 4 || BallNumber == 5 || BallNumber == 6 || BallNumber == 7 || BallNumber == 8);
return true; // if you give it a number higher than 10, it will return true ..
}
Now the method is working fine, but I wanted to prove it.
So I came up with this idea:
int TruePossiblity = 0, FalsePossiblity = 0;
for (int i = 0; i < 100; i++)
{
bool _TRUE = TrueOrFalse(5);
if (_TRUE) TruePossiblity += 1;
else FalsePossiblity += 1;
}
Console.WriteLine(x);
Console.WriteLine(y);
Now, what should be printed is:
50
50
The Problem, is that It either Prints:
100
0
or
0
100
So, I debugged it, and walked through it, step by step, and got amazed that it actually in the end printed:
50
50
I Immediately inserted System.Threading.Thread.Sleep(1);
right after the assignment of the variable _TRUE
I got better results, but not that good, the more I increase the time, the better results I get (closer results to what it should print, which is 50,50).
when I used 200 Millisecs I actually got the required result 50,50 !
Now, There’s only one thread running, which is the main thread, so there’s no race goin on between threads so that things could mess up and give those unwanted results.
What is goin on ?
Could Somebody explain it ?
Can I avoid using the Sleep ?
Thank you for any tips you provide.
A
Randominstance is seeded with the current time. Because the loop executes too fast, it uses the same time, hence you get always the same "random" value.Instead of creating the
Randomin the method itself, you should pass it as argument or use a member variable in the class.For example:
From MSDN remarks: