mercredi 8 avril 2020

Generating random numbers from 1 - 24 in vb.net

I'm trying to randomly generate a list of numbers from 1 - 24 separated by a pipe key. I've manage to get it working except that it generates the same exact number order every time "17|13|14|7|8|19|1|20|18|2|10|21|9|24|23|15|12|16|22|6|3|4|5|11". How do I get it to be truly random?

   Public Function DoesNoAlreadyExistInList(TheNumber As Integer, TheList As String) As Boolean
        Dim counter As Integer = 0
        If (TheList = Nothing Or TheList = "") And Not (TheList.Contains("|")) Then
        Else
            Dim numberlist() As String = TheList.Split("|")
            For Each n As String In numberlist
                Dim n_ As Integer = n
                If n_ = TheNumber Then
                    counter += 1
                Else
                    counter += 0
                End If
            Next
        End If
        If counter = 0 Then
            DoesNoAlreadyExistInList = False
        Else
            DoesNoAlreadyExistInList = True
        End If
End Function


   Protected Sub new_game_Click(sender As Object, e As EventArgs) Handles new_game.Click
        Dim intNo As Integer
        Dim blnExists As Boolean
        Dim counter As Integer = 0
        'Clear the listbox
        player1_cards.Value = Nothing
        'Do 25 times
        For intI = 0 To 23
            'Do this until we no the number is not already in the list
            Do
                'Get a random no 1 - 50
                intNo = Int((24 * Rnd()) + 1)
                'Check if the number is already in the list.
                blnExists = DoesNoAlreadyExistInList(intNo, player1_cards.Value)
            Loop Until blnExists = False
            'Add the number into the listbox.
            counter += 1
            If counter = 1 Then
                player1_cards.Value = player1_cards.Value & intNo
            Else
                player1_cards.Value = player1_cards.Value & "|" & intNo
            End If
        Next
        right_player_number.Text = player1_cards.Value
    End Sub



Aucun commentaire:

Enregistrer un commentaire