Question Random Numbers for Bingo Card

wowjen

Member
Joined
Dec 27, 2009
Messages
13
Programming Experience
Beginner
please, I need a help here...
2q24aah.jpg

;):D;)
 
Array will be initialized in the Form Load event. btnNewGame will shuffle the array. btnNext will select a different number each time it's clicked. Click btnNewGame to reshuffle and start over.



VB.NET:
Public Class Form1
	Private bingo(75) As String
	Private count As Integer

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		For x As Integer = 1 To 75
			Select Case x
				Case 1 To 15
					bingo(x) = "B " & x
				Case 16 To 30
					bingo(x) = "I " & x
				Case 31 To 45
					bingo(x) = "N " & x
				Case 46 To 60
					bingo(x) = "G " & x
				Case 61 To 75
					bingo(x) = "O " & x
			End Select
		Next x
	End Sub

	Private Sub btnNewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewGame.Click
		Dim randnum As New Random()
		Dim mix As Integer, temp As String
		For x As Integer = 1 To 75
			mix = randnum.Next(1, 76)
			temp = bingo(mix)
			bingo(mix) = bingo(x)
			bingo(x) = temp
		Next x
		count = 0
		txtBingo.Clear()
	End Sub

	Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
		count += 1
		txtBingo.Text = bingo(count)
	End Sub
End Class
 
This ensures that you have values that you need for each column. I made a form with 25 labels on it and changed the Tag property. You could easily use the Name property as well.
VB.NET:
 Private Sub frmBingo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label1.Tag = "B1"
        Label2.Tag = "B2"
        Label3.Tag = "B3"
        Label4.Tag = "B4"
        Label5.Tag = "B5"
        Label6.Tag = "I1"
        Label7.Tag = "I2"
        Label8.Tag = "I3"
        Label9.Tag = "I4"
        Label10.Tag = "I5"
        Label11.Tag = "N1"
        Label12.Tag = "N2"
        Label13.Tag = "N3"
        Label14.Tag = "N4"
        Label15.Tag = "N5"
        Label16.Tag = "G1"
        Label17.Tag = "G2"
        Label18.Tag = "G3"
        Label19.Tag = "G4"
        Label20.Tag = "G5"
        Label21.Tag = "O1"
        Label22.Tag = "O2"
        Label23.Tag = "O3"
        Label24.Tag = "O4"
        Label25.Tag = "O5"

        NewCard()

    End Sub


    Private Sub NewCard()

        For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is Label Then
                Dim TempLabel As Label = DirectCast(ctrl, Label)
                Select Case TempLabel.Tag.ToString()(0)
                    Case "B"c
                        TempLabel.Text = TempLabel.Tag.ToString()(0) & rRnd.Next(1, 16).ToString()

                    Case "I"c
                        TempLabel.Text = TempLabel.Tag.ToString()(0) & rRnd.Next(16, 31).ToString()
                    Case "N"c
                        If TempLabel.Tag.ToString().Substring(1) = "3" Then
                            TempLabel.Text = "Free"
                        Else
                            TempLabel.Text = TempLabel.Tag.ToString()(0) & rRnd.Next(31, 46).ToString()
                        End If
                    Case "G"c
                        TempLabel.Text = TempLabel.Tag.ToString()(0) & rRnd.Next(46, 61).ToString()
                    Case "O"c
                        TempLabel.Text = TempLabel.Tag.ToString()(0) & rRnd.Next(61, 76).ToString()

                End Select
            End If
        Next

    End Sub

   
    Private Sub btnNewCard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewCard.Click
        NewCard()
    End Sub
 
thanks

:):):)@ Solitaire ::):):)

Array will be initialized in the Form Load event. btnNewGame will shuffle the array. btnNext will select a different number each time it's clicked. Click btnNewGame to reshuffle and start over.



VB.NET:
Public Class Form1
	Private bingo(75) As String
	Private count As Integer

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		For x As Integer = 1 To 75
			Select Case x
				Case 1 To 15
					bingo(x) = "B " & x
				Case 16 To 30
					bingo(x) = "I " & x
				Case 31 To 45
					bingo(x) = "N " & x
				Case 46 To 60
					bingo(x) = "G " & x
				Case 61 To 75
					bingo(x) = "O " & x
			End Select
		Next x
	End Sub

	Private Sub btnNewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewGame.Click
		Dim randnum As New Random()
		Dim mix As Integer, temp As String
		For x As Integer = 1 To 75
			mix = randnum.Next(1, 76)
			temp = bingo(mix)
			bingo(mix) = bingo(x)
			bingo(x) = temp
		Next x
		count = 0
		txtBingo.Clear()
	End Sub

	Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
		count += 1
		txtBingo.Text = bingo(count)
	End Sub
End Class


I can write the codes for generating random number in the textbox as :

Private Sub btnRandom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRandom.Click
Randomize()
txtRandom.Text = Int(Rnd() * 75)
End Sub

thanks anyway! :):):)
I added this to your reputation!:):):)
 
demausdauth: You don't need all those tags. All you need is the Text property of the labels.

wowjen: You DO need all that code. The Shuffle Sort algorithm I described above will result in 75 unique selections with no repeats, using a form-level variable to remember all the previous selections. If you run your code long enough, you will find that one or more of the numbers are repeated before the game is over.

Furthermore, you should be using the new Random object, not the legacy Rnd function.
 
Last edited:
thanks

:):):)@ dermausdauth:):):)

I appreciate your help,
but what I need is to generate a random number
without repeating in every column of BINGO Card...
and it ranges:

B = 1 to 15
I = 16 to 30
N = 31 to 45
G = 36 to 60
O = 61 to 75

:):):)btw, thanks for your help:):):)
 
wowjen: Take another look at the previous post, which I edited a short time ago. The code I posted for you creates an array with 75 unique numbers that do not repeat. You need to study it and apply it to your own program.


Previous post:

wowjen: You DO need all that code. The Shuffle Sort algorithm I described above will result in 75 unique selections with no repeats, using a form-level variable to remember all the previous selections. If you run your code long enough, you will find that one or more of the numbers are repeated before the game is over.

Furthermore, you should be using the new Random object, not the legacy Rnd function.
 

Latest posts

Back
Top