| | |
Flipping two coins.
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
1
#2 Oct 11th, 2009
Random is time-seeded. Use the
It gave a pretty fair roll:
Calling it:
RNGCryptoServiceProvider : C# Syntax (Toggle Plain Text)
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:
text Syntax (Toggle Plain Text)
[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:
C# Syntax (Toggle Plain Text)
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); }
Last edited by sknake; Oct 11th, 2009 at 7:52 am.
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
Make love, no war. Cave ab homine unius libri.
Danny
![]() |
Similar Threads
- Is The Gov't Getting Rid Of Cash And Coins (Geeks' Lounge)
- heads or tails (C)
- make chance - least coins algorithm (C)
Other Threads in the C# Forum
- Previous Thread: How to get md5 value or alike from arraylist?
- Next Thread: Getting a dynamic IP address
| Thread Tools | Search this Thread |
.net access advice algorithm analytics array asp.net broadcast c# c++ camera combobox commerce community connection contorl control conversion cs4 data database datagrid datastructure datetime decimal degrees developer development directrobot disabled drawing ecommerce editor enum equation error file form formatting forms function gadgets gdi+ google http httpwebrequest index internet javascript keypress label list listview mandelbrot marshalbyrefobject math messagebox mysql news operator oracle password path photoshop php post prime programmer programming radians recursive remoting resourcefile richtextbox search silverlight smoobjects sql sql-server sqlserver statistics string tables text textbox totaldays twitter user usercontrol validation vb visual visual-studio visualbasic webdevelopemnt webdevelopment winforms wolframalpha wpf write







