Hello all, I'm having some trouble with a project I am doing and I was wandering if anybody could help me/point me in the right direction.

So the gist of what is happening is I have a program that is supposed to be flipping 2 coins independently of each other but each coin always mirrors the others result.

Hear is the 2 pieces of code.

first of all the code to flip the coin.

    class Coin {
        private const int HEADS = 0;
        private const int TAILS = 1;

        private int face;

        /// <summary>
        /// Random class object used to generate random numbers
        /// Look at Random class for description of methods available
        /// </summary>
        private Random random = new Random();

        //-----------------------------------------------------------------
        //  Sets up the coin by flipping it initially.
        //-----------------------------------------------------------------
        public Coin() {
            Flip();
        }

        //-----------------------------------------------------------------
        //  Flips the coin by randomly choosing a face value.
        //-----------------------------------------------------------------
        public void Flip() {
            face = random.Next(2);
        }

        //-----------------------------------------------------------------
        //  Returns true if the current face of the coin is heads.
        //-----------------------------------------------------------------
        public bool IsHeads() {
            return (face == HEADS);
        }

        //-----------------------------------------------------------------
        //  Returns the current face of the coin as a string.
        //-----------------------------------------------------------------
        public override string ToString() {
            string faceName;

            if (IsHeads()) {
                faceName = "Heads";
            } else {
                faceName = "Tails";
            }
            return faceName;
        }
    }
}

and here is the code to flip two coins

class TwoUp : Coin {

        private Coin coin1 = new Coin();
        private Coin coin2 = new Coin();

Anybody now why the mirror the same results 100% of the time, i.e.
tails tails / heads heads and never a combination of the two.

Random is time-seeded. Use the RNGCryptoServiceProvider :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace daniweb
{
  public class Coin
  {
    private Side _lastRoll;
    public Side LastRoll { get { return _lastRoll; } }

    public enum Side 
    { 
      Heads = 0,
      Tails = 1
    }

    public Coin()
    {
    }

    public Side Roll()
    {
      int rnd = GetRandom();
      _lastRoll = (Side)rnd;
      return LastRoll;
    }

    private static int GetRandom()
    {
      byte[] randomNumber = new byte[1];
      System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
      Gen.GetBytes(randomNumber);
      int result = Convert.ToInt32(randomNumber[0]);
      if ((result & 0x1) == 0x1)
        return 1;
      else
        return 0;
    }
  }
}

It gave a pretty fair roll:

[00] Side: Tails
[01] Side: Heads
[02] Side: Heads
[03] Side: Tails
[04] Side: Heads
[05] Side: Heads
[06] Side: Tails
[07] Side: Tails
[08] Side: Heads
[09] Side: Tails
[10] Side: Heads
[11] Side: Tails
[12] Side: Tails
[13] Side: Heads
[14] Side: Tails
[15] Side: Tails
[16] Side: Tails
[17] Side: Tails
[18] Side: Tails
[19] Side: Tails
[20] Side: Heads
[21] Side: Heads
[22] Side: Heads
[23] Side: Tails
[24] Side: Tails
[25] Side: Heads
[26] Side: Tails
[27] Side: Heads
[28] Side: Tails
[29] Side: Heads
[30] Side: Heads
[31] Side: Tails
[32] Side: Heads
[33] Side: Heads
[34] Side: Heads
[35] Side: Heads
[36] Side: Tails
[37] Side: Heads
[38] Side: Heads
[39] Side: Heads
[40] Side: Heads
[41] Side: Heads
[42] Side: Tails
[43] Side: Heads
[44] Side: Tails
[45] Side: Tails
[46] Side: Heads
[47] Side: Tails
[48] Side: Heads
[49] Side: Tails
--
Total heads: 26
Total tails: 24

Calling it:

private void button3_Click(object sender, EventArgs e)
    {
      Coin c = new Coin();
      int headCnt = 0;
      int tailCnt = 0;

      for (int i1 = 0; i1 < 50; i1++)
      {
        Coin.Side side = c.Roll();
        Console.WriteLine(string.Format("[{0}] Side: {1}",
          i1.ToString().PadLeft(2, '0'),
          side.ToString()));
        if (side == Coin.Side.Heads)
          headCnt++;
        else
          tailCnt++;
      }
      Console.WriteLine("--");
      Console.WriteLine("Total heads: {0:F0}", headCnt);
      Console.WriteLine("Total tails: {0:F0}", tailCnt);
    }

You are calling new Random() so close after each other you will get the same random sequence every time. Try to use it once and than call the Next() method. Or better still follow the advice of sknake!

Use private static Random random = new Random(); so that the same random number generator is used across different instances of Coin.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.