Coin Toss Help Please

seanvoth

Well-known member
Joined
Aug 5, 2006
Messages
58
Programming Experience
1-3
I have a textbox and a button
i want it so when i click on the button it
will (rand) pick heads or tails
just like a coin toss:confused:
 
declare a random object, then use that to pick a number between 0 and 2, with 0 being heads and 1 being tails, then use a select case
 
a little code please becuse i dont get what u said srry im only 14 and im starting to get all this now
 
I quickly did this, i hope it helps. I havent tested it though:

VB.NET:
        'Declare sideUp as a Random which will eventually be either 0 or 1
        Dim sideUp As New Random
        'Declare coinSide as a Number (Integer) and set it to either 0 (Heads) or 1 (Tails) randomly.
        Dim coinSide As Integer = sideUp.Next(0, 2)

        'Set the text in the textbox to heads or tails based on the number chosen above
        Select Case coinSide
            Case 0
                'Set the text in the textbox to heads
                TextBox1.Text = "Heads"
            Case 1
                'Set the text in the textbox to tails
                TextBox1.Text = "Tails"
        End Select
 
Thank You it Worked and it dose Rand but it dose a pattern h,t,t,t,t,t,h,t,t,t,t,t how do i stop that
 
Last edited:
Well, you could make it do a random number up to 10 and have all the evens tails and all teh olds heads?
 
Hello.
Double-click the form to display the Form_Load procedure. In it, type
VB.NET:
Randomize
This randomize statement is added to the program and will be executed each time the program starts. Randomize uses the system clock to create a truly random starting point for the Rnd statement.
Now it won't produce the same string of random spins.
OK.
 
Hello.
Double-click the form to display the Form_Load procedure. In it, type
VB.NET:
Randomize
This randomize statement is added to the program and will be executed each time the program starts. Randomize uses the system clock to create a truly random starting point for the Rnd statement.
Now it won't produce the same string of random spins.
OK.

Do you have to specify the random for this?
 
VB.NET:
    Private rand As New Random

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Select Case rand.Next(0, 100) Mod 2
            Case 0 : Label1.Text = "Heads"
            Case 1 : Label1.Text = "Tails"
        End Select
    End Sub
 
Err... im still fairly new, but I was taught this works;

This is assuming that VB gives real randoms, which it doesnt... but for a coin flip, it works.
VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] Form1[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] Coin [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] RandomGenerator [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Random[/SIZE]
[SIZE=2]Coin = RandomGenerator.Next(0, 2)[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Coin = 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Label1.Text = [/SIZE][SIZE=2][COLOR=#800000]"Heads"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] Coin = 1 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Label1.Text = [/SIZE][SIZE=2][COLOR=#800000]"Tails"[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
 
Last edited:
Back
Top