AngryMaking a randomizer...

Posts 1–15 of 41 · Page 1 of 3
Making a randomizer...
OK so I have this code but I have no idea how to make it random... I though I could but the code is really wierd. Its for my Deal or No Deal game, heres the code of one of the things... I think its really bad and thats why I cant figure it out

 
Code
Code:
Private Sub case5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles case5.Click
        If gamedecidetb.Text = "1" Then
           
            If sidebar1.Enabled = False And sidebar2.Enabled = False And sidebar3.Enabled = False And sidebar4.Enabled = True And sidebar5.Enabled = False And sidebar6.Enabled = False And sidebar7.Enabled = False And sidebar8.Enabled = False And sidebar9.Enabled = False And sidebar10.Enabled = False And sidebar11.Enabled = False And sidebar12.Enabled = False And sidebar13.Enabled = False And sidebar14.Enabled = False And sidebar15.Enabled = False And sidebar16.Enabled = False And sidebar17.Enabled = False And sidebar18.Enabled = False And sidebar19.Enabled = False And sidebar20.Enabled = False And sidebar21.Enabled = False And sidebar22.Enabled = False And sidebar23.Enabled = False And sidebar24.Enabled = False And sidebar25.Enabled = False And sidebar26.Enabled = False And gamedecidetb.Text = "1" Then
                MsgBox("You have won $10.")
                playagain()
            End If
          
            If sidebar1.Enabled = False And sidebar2.Enabled = False And sidebar3.Enabled = False And sidebar4.Enabled = False And sidebar5.Enabled = False And sidebar6.Enabled = False And sidebar7.Enabled = False And sidebar8.Enabled = False And sidebar9.Enabled = False And sidebar10.Enabled = False And sidebar11.Enabled = False And sidebar12.Enabled = False And sidebar13.Enabled = False And sidebar14.Enabled = False And sidebar15.Enabled = False And sidebar16.Enabled = False And sidebar17.Enabled = False And sidebar18.Enabled = False And sidebar19.Enabled = False And sidebar20.Enabled = False And sidebar21.Enabled = False And sidebar22.Enabled = False And sidebar23.Enabled = False And sidebar24.Enabled = False And sidebar25.Enabled = False And sidebar26.Enabled = False And gamedecidetb.Text = "1" Then
                playagain()
            End If
            sidebar4.Enabled = False
            case5.Enabled = False
        End If
        caseleft = caseleft - 1
        counter = counter + 1
        total = total - 10
        If counter = 5 Then
            bankoffer = total / caseleft
            yesno = MsgBox("The banker offers " & bankoffer, vbYesNo)
            If yesno = vbYes Then
                MsgBox("You have won " & bankoffer)
                playagain()
            Else
            End If
        End If

        If counter = 10 Then
            bankoffer = total / caseleft
            yesno = MsgBox("The banker offers " & bankoffer, vbYesNo)
            If yesno = vbYes Then
                MsgBox("You have won " & bankoffer)
                playagain()
            Else
            End If
        End If

        If counter = 14 Then
            bankoffer = total / caseleft
            yesno = MsgBox("The banker offers " & bankoffer, vbYesNo)
            If yesno = vbYes Then
                MsgBox("You have won " & bankoffer)
                playagain()
            Else
            End If
        End If

        If counter = 18 Then
            bankoffer = total / caseleft
            yesno = MsgBox("The banker offers " & bankoffer, vbYesNo)
            If yesno = vbYes Then
                MsgBox("You have won " & bankoffer)
                playagain()
            Else
            End If
        End If

        If counter = 21 Then
            bankoffer = total / caseleft
            yesno = MsgBox("The banker offers " & bankoffer, vbYesNo)
            If yesno = vbYes Then
                MsgBox("You have won " & bankoffer)
                playagain()
            Else
            End If
        End If

        If counter = 24 Then
            bankoffer = total / caseleft
            yesno = MsgBox("The banker offers " & bankoffer, vbYesNo)
            If yesno = vbYes Then
                MsgBox("You have won " & bankoffer)
                playagain()
            Else
            End If
        End If
    End Sub



The gamedecidetb was a failed attemp at making it random )':
Wish i could understand visual basic more.
What are the names of the boxes
Quote Originally Posted by xMeltdowNx View Post
What are the names of the boxes
The cases are buttons
This Will Shuffle The 26 Numbers, You Should Be Able To Work This In Easy Enough.

Declare This At The Top Of Your Class:
Code:
Private caseArray(25) As Integer
Here Is The Shuffle Sub:
Code:
Private Sub reloadCase()
        Dim getRand As random =New Random()
        'Resets case numbers in the array e.g. caseArray(0) = 1 etc.
        For i = 0 To 25
            caseArray(i) = i + 1
        Next
        'Shuffles the array
        For i = 0 To caseArray.Count - 1
            'Gets a random number up to 26
            Dim l As Integer = getRand.Next(caseArray.Count)
            'Temporarily holds the value of the random index
            Dim t As Integer = caseArray(l)
            'Swaps the random index with the current index
            caseArray(l) = caseArray(i)
            'Gives the current index the old value of the random index
            caseArray(i) = t
        Next
    End Sub
Example (Below Probably Wont Work As I Dont Know Your Code):

Code:
Private Sub case5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles case5.Click
        Dim caseContent As Integer = caseArray(5)
        Select Case caseContent
            Case 1
                'Your 'sidebar' code here
            Case 2

            Case 3

            Case 4


                'Keep Going Until Case 26
        End Select
    End Sub
Quote Originally Posted by xMeltdowNx View Post
This Will Shuffle The 26 Numbers, You Should Be Able To Work This In Easy Enough.

Declare This At The Top Of Your Class:
Code:
Private caseArray(25) As Integer
Here Is The Shuffle Sub:
Code:
Private Sub reloadCase()
        Dim getRand As random =New Random()
        'Resets case numbers in the array e.g. caseArray(0) = 1 etc.
        For i = 0 To 25
            caseArray(i) = i + 1
        Next
        'Shuffles the array
        For i = 0 To caseArray.Count - 1
            'Gets a random number up to 26
            Dim l As Integer = getRand.Next(caseArray.Count)
            'Temporarily holds the value of the random index
            Dim t As Integer = caseArray(l)
            'Swaps the random index with the current index
            caseArray(l) = caseArray(i)
            'Gives the current index the old value of the random index
            caseArray(i) = t
        Next
    End Sub
Example (Below Probably Wont Work As I Dont Know Your Code):

Code:
Private Sub case5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles case5.Click
        Dim caseContent As Integer = caseArray(5)
        Select Case caseContent
            Case 1
                'Your 'sidebar' code here
            Case 2

            Case 3

            Case 4


                'Keep Going Until Case 26
        End Select
    End Sub
Make sure you seed your random or the numbers will be useless.
I seed mine like such:
Code:
class form1
    Dim time As Stopwatch = New Stopwatch
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        time.Start()
    End Sub
    ' Now for your code:
    Private Sub reloadCase()
        Dim getRand As random =New Random(time.ElapsedMilliseconds)
        'Resets case numbers in the array e.g. caseArray(0) = 1 etc.
        For i = 0 To caseArray.length ' more universal.
            caseArray(i) = i + 1
        Next
        'Shuffles the array
        For i = 0 To caseArray.Count - 1
            'Gets a random number up to 26
            Dim l As Integer = getRand.Next(caseArray.Count)
            'Temporarily holds the value of the random index
            Dim t As Integer = caseArray(l)
            'Swaps the random index with the current index
            caseArray(l) = caseArray(i)
            'Gives the current index the old value of the random index
            caseArray(i) = t
        Next
    End Sub
OKay so far I have this but I have no idea what to do now. Heres part of the code(not in this order)


Code:
 
Code
Private caseArray(25) As Integer Private Sub playagain() sidebar1.Enabled = True sidebar2.Enabled = True sidebar3.Enabled = True sidebar4.Enabled = True sidebar5.Enabled = True sidebar6.Enabled = True sidebar7.Enabled = True sidebar8.Enabled = True sidebar9.Enabled = True sidebar10.Enabled = True sidebar11.Enabled = True sidebar12.Enabled = True sidebar13.Enabled = True sidebar14.Enabled = True sidebar15.Enabled = True sidebar16.Enabled = True sidebar17.Enabled = True sidebar18.Enabled = True sidebar19.Enabled = True sidebar20.Enabled = True sidebar21.Enabled = True sidebar22.Enabled = True sidebar23.Enabled = True sidebar24.Enabled = True sidebar25.Enabled = True sidebar26.Enabled = True case1.Enabled = True case2.Enabled = True case3.Enabled = True case4.Enabled = True case5.Enabled = True case6.Enabled = True case7.Enabled = True case8.Enabled = True case8.Enabled = True case9.Enabled = True case10.Enabled = True case11.Enabled = True case12.Enabled = True case13.Enabled = True case14.Enabled = True case15.Enabled = True case16.Enabled = True case17.Enabled = True case18.Enabled = True case19.Enabled = True case20.Enabled = True case21.Enabled = True case22.Enabled = True case23.Enabled = True case24.Enabled = True case25.Enabled = True case26.Enabled = True playstartmusic() counter = 0 bankoffer = 0 total = 3416000 caseleft = 26 btnhelp.Visible = True reloadCase() End Sub Private Sub reloadCase() Dim getRand As random = New Random() 'Resets case numbers in the array e.g. caseArray(0) = 1 etc. For i = 0 To 25 caseArray(i) = i + 1 Next 'Shuffles the array For i = 0 To caseArray.Count - 1 'Gets a random number up to 26 Dim l As Integer = getRand.Next(caseArray.Count) 'Temporarily holds the value of the random index Dim t As Integer = caseArray(l) 'Swaps the random index with the current index caseArray(l) = caseArray(i) 'Gives the current index the old value of the random index caseArray(i) = t Next End Sub
Well congrats, your reload cases will always "randomize" the cases in the same order. Read my post or they'll always be in the same order.
Quote Originally Posted by LilGho$t View Post
Well congrats, your reload cases will always "randomize" the cases in the same order. Read my post or they'll always be in the same order.
What are you talking about? It randomizes it every time you just added some unnecessary code which makes no difference.

To prove this I made an example (below)

Virustotal
Jotti
Randomize Test_mpgh.net.zip7 KB · 7 downloads Scanning…
From this thread, and the other thread, it sounds like you want to make a deal or no deal game.

This is what I'd do for the cases and such
Code:
Public Class Form1
    Dim cases As New List(Of ULong)

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cases.AddRange({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) 'etc
    End Sub
    Private Sub case_clicked(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click 'etc
        sender.enabled = False
        sender.hide()
        MsgBox(case_val)
    End Sub
    Private Function case_val()
        Dim value_of_case As New Random
        Dim index As ULong = value_of_case.Next(0, cases.Count - 1)
        Dim returnable As ULong = cases(index)
        cases.RemoveAt(index)
        Return returnable
    End Function
End Class
Quote Originally Posted by rileyjstrickland View Post
From this thread, and the other thread, it sounds like you want to make a deal or no deal game.

This is what I'd do for the cases and such
Code:
Public Class Form1
    Dim cases As New List(Of ULong)

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cases.AddRange({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) 'etc
    End Sub
    Private Sub case_clicked(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click 'etc
        sender.enabled = False
        sender.hide()
        MsgBox(case_val)
    End Sub
    Private Function case_val()
        Dim value_of_case As New Random
        Dim index As ULong = value_of_case.Next(0, cases.Count - 1)
        Dim returnable As ULong = cases(index)
        cases.RemoveAt(index)
        Return returnable
    End Function
End Class
You 'case_val' function will cause duplicates (there will be two or more of the same numbers)
Quote Originally Posted by xMeltdowNx View Post
You 'case_val' function will cause duplicates (there will be two or more of the same numbers)
Um No?
If you read the actual code, fully, you would've seen the small portion where I remove the number from the list AFTER it's selected.

Example
Before Click
1,2,3
After Click
1,3
Quote Originally Posted by rileyjstrickland View Post
Um No?
If you read the actual code, fully, you would've seen the small portion where I remove the number from the list AFTER it's selected.

Example
Before Click
1,2,3
After Click
1,3
That will remove the case but the same 'random' number will most likely be generated twice.
Quote Originally Posted by xMeltdowNx View Post
That will remove the case but the same 'random' number will most likely be generated twice.
But the "same" doesn't matter. Just as long as the money values are random.

Example

1,2,3 (if number is '1' then returned value is '2')
1,3 (if number is '1' then returned value is '3')
Quote Originally Posted by rileyjstrickland View Post
But the "same" doesn't matter. Just as long as the money values are random.

Example

1,2,3 (if number is '1' then returned value is '2')
1,3 (if number is '1' then returned value is '3')
Yes but the 'same' means it will give you the same box value more than once e.g. you will get 2000 twice and the money values aren't random, there selection is.
Posts 1–15 of 41 · Page 1 of 3

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?