Flipping two coins.

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 2
Reputation: Daiosmith is an unknown quantity at this point 
Solved Threads: 0
Daiosmith Daiosmith is offline Offline
Newbie Poster

Flipping two coins.

 
0
  #1
Oct 11th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
1
  #2
Oct 11th, 2009
Random is time-seeded. Use the RNGCryptoServiceProvider :
  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:
  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:
  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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,909
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 274
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
0
  #3
Oct 11th, 2009
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!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter
 
1
  #4
Oct 11th, 2009
Use private static Random random = new Random(); so that the same random number generator is used across different instances of Coin.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Reply

Tags
c#, math, statistics

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC