2

I have been playing around and wrote this little piece of code. I am trying to flip a coin defined number of times and then count how many tails and heads I am getting. So here it is:

private void Start_Click(object sender, EventArgs e)
{
    int headss = 0;
    int tailss = 0;
    int random2, g;
    string i = textBox1.Text;
    int input2, input;
    bool NumberCheck = int.TryParse(i, out input2);

    if (textBox1.Text == String.Empty) // check for empty string, when true
        MessageBox.Show("Enter a valid number between 0 and 100000.");
    else // check for empty string, when false
        if (!NumberCheck) // number check, when false
        {
            textBox1.Text = String.Empty;
            MessageBox.Show("Enter a valid number between 0 and 100000.");
        }
        else
        {
            input = Convert.ToInt32(textBox1.Text);

            for (g = 0; g < input; g++)
            {
                Random random = new Random();
                random2 = random.Next(2);

                if (random2 == 0)
                {
                    headss++;
                }
                else if (random2 == 1)
                {
                    tailss++;
                }
            }
        }

    heads.Text = Convert.ToString(headss);
    tails.Text = Convert.ToString(tailss);
}

The problem is that I keep getting problems while displaying the content. It's not even close to display they right result. Any ideas?

EDIT. Solution: move following line 3 lines up :D

Random random = new Random();
HelpNeeder
  • 5,933
  • 21
  • 80
  • 141
  • 5
    The display seems fine, but the flipping is not fine. `Random`'s default seed is based on the system clock, whose resolution is not that great. By creating a new `Random` object each time in the loop, you'll get the same seed over and over for many iterations, so your flips will not be very random. – mqp Apr 25 '11 at 05:47

4 Answers4

6

Instead of

for (g = 0; g < input; g++)
{
   Random random = new Random();
   random2 = random.Next(2);
}

Declare a single Random for use throughout:

private Random randomGenerator = new Random();
private void Start_Click(object sender, EventArgs e)
{
    // ...
    for (g = 0; g < input; g++)
    {
        random2 = randomGenerator.Next(2);
    }
    // ...
}
bendewey
  • 38,066
  • 11
  • 94
  • 122
4

You should use only one Random object to generate good (as good as default Random does) random sequence.

oxilumin
  • 4,565
  • 2
  • 16
  • 25
3

The default constructor for random take the systmem time as seed. Therefore, if you generate lots of them in a short amount of time they will all generate the same sequence of random numbers. Pull the random object out of the loop and this effect will not occur.

Howard
  • 36,183
  • 6
  • 60
  • 81
0

With RandomGenerator. This code will count how many times coin has been flipped. It will end with 3 consecutive HEADS.

private RandomGenerator rgen = new RandomGenerator ();

public void run () {

    int value = 0;
    int total = 0;
    while (value != 3) {
        String coinFlip =  rgen.nextBoolean() ? "HEADS" : "TAILS";
        println (coinFlip);
        if (coinFlip == "HEADS") {
           value+=1;
        } else {
           value=0;
        }
        total +=1;
    }
    println ("It took "+total+" flips to get 3 consecutive heads");     
}
pirho
  • 8,805
  • 12
  • 34
  • 44