944,145 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 2500
  • C# RSS
Oct 11th, 2009
0

Flipping two coins.

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Daiosmith is offline Offline
2 posts
since Oct 2008
Oct 11th, 2009
1
Re: Flipping two coins.
Random is time-seeded. Use the RNGCryptoServiceProvider :
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace daniweb
  7. {
  8. public class Coin
  9. {
  10. private Side _lastRoll;
  11. public Side LastRoll { get { return _lastRoll; } }
  12.  
  13. public enum Side
  14. {
  15. Heads = 0,
  16. Tails = 1
  17. }
  18.  
  19. public Coin()
  20. {
  21. }
  22.  
  23. public Side Roll()
  24. {
  25. int rnd = GetRandom();
  26. _lastRoll = (Side)rnd;
  27. return LastRoll;
  28. }
  29.  
  30. private static int GetRandom()
  31. {
  32. byte[] randomNumber = new byte[1];
  33. System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider();
  34. Gen.GetBytes(randomNumber);
  35. int result = Convert.ToInt32(randomNumber[0]);
  36. if ((result & 0x1) == 0x1)
  37. return 1;
  38. else
  39. return 0;
  40. }
  41. }
  42. }

It gave a pretty fair roll:
text Syntax (Toggle Plain Text)
  1. [00] Side: Tails
  2. [01] Side: Heads
  3. [02] Side: Heads
  4. [03] Side: Tails
  5. [04] Side: Heads
  6. [05] Side: Heads
  7. [06] Side: Tails
  8. [07] Side: Tails
  9. [08] Side: Heads
  10. [09] Side: Tails
  11. [10] Side: Heads
  12. [11] Side: Tails
  13. [12] Side: Tails
  14. [13] Side: Heads
  15. [14] Side: Tails
  16. [15] Side: Tails
  17. [16] Side: Tails
  18. [17] Side: Tails
  19. [18] Side: Tails
  20. [19] Side: Tails
  21. [20] Side: Heads
  22. [21] Side: Heads
  23. [22] Side: Heads
  24. [23] Side: Tails
  25. [24] Side: Tails
  26. [25] Side: Heads
  27. [26] Side: Tails
  28. [27] Side: Heads
  29. [28] Side: Tails
  30. [29] Side: Heads
  31. [30] Side: Heads
  32. [31] Side: Tails
  33. [32] Side: Heads
  34. [33] Side: Heads
  35. [34] Side: Heads
  36. [35] Side: Heads
  37. [36] Side: Tails
  38. [37] Side: Heads
  39. [38] Side: Heads
  40. [39] Side: Heads
  41. [40] Side: Heads
  42. [41] Side: Heads
  43. [42] Side: Tails
  44. [43] Side: Heads
  45. [44] Side: Tails
  46. [45] Side: Tails
  47. [46] Side: Heads
  48. [47] Side: Tails
  49. [48] Side: Heads
  50. [49] Side: Tails
  51. --
  52. Total heads: 26
  53. Total tails: 24

Calling it:
C# Syntax (Toggle Plain Text)
  1. private void button3_Click(object sender, EventArgs e)
  2. {
  3. Coin c = new Coin();
  4. int headCnt = 0;
  5. int tailCnt = 0;
  6.  
  7. for (int i1 = 0; i1 < 50; i1++)
  8. {
  9. Coin.Side side = c.Roll();
  10. Console.WriteLine(string.Format("[{0}] Side: {1}",
  11. i1.ToString().PadLeft(2, '0'),
  12. side.ToString()));
  13. if (side == Coin.Side.Heads)
  14. headCnt++;
  15. else
  16. tailCnt++;
  17. }
  18. Console.WriteLine("--");
  19. Console.WriteLine("Total heads: {0:F0}", headCnt);
  20. Console.WriteLine("Total tails: {0:F0}", tailCnt);
  21. }
Last edited by sknake; Oct 11th, 2009 at 7:52 am.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 11th, 2009
0
Re: Flipping two coins.
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!
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Oct 11th, 2009
3
Re: Flipping two coins.
Use private static Random random = new Random(); so that the same random number generator is used across different instances of Coin.
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is online now Online
2,480 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: How to get md5 value or alike from arraylist?
Next Thread in C# Forum Timeline: Getting a dynamic IP address





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC