Question

I have a form application that creates 10 random numbers and then plays different guitar strings based off of that list of number, but every time i run it, there is always at least 3 back to back repeats of a number. The application writes to a text file so here is an example

 Public Class Form1
Public s As String
Dim x As New List(Of String)
Dim writer As StreamWriter = New StreamWriter("C:\Users\rmonzing\Desktop\Chords.txt")

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Timer1.Start()

End Sub

Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles MyBase.FormClosed

    For Each item In x
        writer.Write(item.ToString)
    Next

    writer.Close()

End Sub

Public Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim range As Integer = 10

    s = "Strings Played  "
    s += vbNewLine
    Try

        For i = 0 To range
            Dim rand As New Random

            Dim z = rand.Next(1, 10)

            If z = 1 Then
                AxWindowsMediaPlayer1.URL = "C:\Users\rmonzing\Documents\GuitarChord-A_64kb_mp3\1st_String_E_64kb.mp3"
                s += "Chord A - String E    "
            ElseIf z = 2 Then
                AxWindowsMediaPlayer2.URL = "C:\Users\rmonzing\Documents\GuitarChord-A_64kb_mp3\2nd_String_B__64kb.mp3"
                s += "Chord A - String B   "
            ElseIf z = 3 Then
                AxWindowsMediaPlayer3.URL = "C:\Users\rmonzing\Documents\GuitarChord-A_64kb_mp3\3rd_String_G_64kb.mp3"
                s += "Chord A - String G   "
            ElseIf z = 4 Then
                AxWindowsMediaPlayer4.URL = "C:\Users\rmonzing\Documents\GuitarChord-A_64kb_mp3\4th_String_D_64kb.mp3"
                s += "Chord A - String D   "
            ElseIf z = 5 Then
                AxWindowsMediaPlayer5.URL = "C:\Users\rmonzing\Documents\GuitarChord-A_64kb_mp3\5th_String_A_64kb.mp3"
                s += "Chord A - String A   "
            ElseIf z = 6 Then
                AxWindowsMediaPlayer6.URL = "C:\Users\rmonzing\Documents\GuitarChord-A_64kb_mp3\6th_String_E_64kb.mp3"
                s += "Chord A - String E2   "
            ElseIf z = 7 Then
                AxWindowsMediaPlayer7.URL = "C:\Users\rmonzing\Documents\GuitarChord-A_64kb_mp3\C_64kb.mp3"
                s += "Chord A - String C   "
            ElseIf z = 8 Then
                AxWindowsMediaPlayer8.URL = "C:\Users\rmonzing\Documents\GuitarChord-A_64kb_mp3\D_64kb.mp3"
                s += "Chord A - String D Major   "
            ElseIf z = 9 Then
                AxWindowsMediaPlayer9.URL = "C:\Users\rmonzing\Documents\GuitarChord-A_64kb_mp3\Dm_64kb.mp3"
                s += "Chord A - String D Minor  "
            ElseIf z = 10 Then
                AxWindowsMediaPlayer10.URL = "C:\Users\rmonzing\Documents\GuitarChord-A_64kb_mp3\E_64kb.mp3"
                s += "Chord A - String E Major  "
            End If
            s += vbNewLine
            z = Nothing
        Next
        CollectChords(s)

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try



End Sub

Public Sub CollectChords(ByVal s As String)

    x.Add(s)

End Sub

End Class

This is the whole project, but every time i run it, i get a text file like this

Strings Played
Chord A - String E2
Chord A - String G
Chord A - String B
Chord A - String A
Chord A - String G
Chord A - String G
Chord A - String C
Chord A - String C
Chord A - String C
Chord A - String E
Chord A - String E
Strings Played
Chord A - String D Major
Chord A - String G
Chord A - String E
Chord A - String D
Chord A - String D
Chord A - String G
Chord A - String G
Chord A - String E2
Chord A - String E2
Chord A - String D Minor
Chord A - String D Minor
Strings Played
Chord A - String B
Chord A - String E
Chord A - String D
Chord A - String B
Chord A - String E2
Chord A - String D Minor
Chord A - String D Minor
Chord A - String C
Chord A - String C
Chord A - String B
Chord A - String B
Strings Played
Chord A - String D Minor
Chord A - String D Minor
Chord A - String G
Chord A - String B
Chord A - String B
Chord A - String A
Chord A - String D Major
Chord A - String D Major
Chord A - String C
Chord A - String C
Chord A - String E
Strings Played
Chord A - String G
Chord A - String E2
Chord A - String E2
Chord A - String A
Chord A - String D Major
Chord A - String D Major
Chord A - String E2
Chord A - String E2
Chord A - String E2
Chord A - String E
Chord A - String E

Doesnt look random to me, no idea whats going on here..

Was it helpful?

Solution

Change it to a static variable outside the loop.

Static rand As New Random

The Random class uses a seed (which is just an integer) to generate its Random numbers. Two Random objects with the same seed will generate the same numbers.

Now, if you don't provide a seed in the constructor, the default is that the seed is taken from Environment.TickCount.

Therefore, if you create multiple Random objects in quick succession, it's likely that many/all of the Random objects will get the same seed value from Environment.TickCount, and thus will generate the same sequence of numbers.

For this reason, it is generally advised that you have only one instance of Random in your application (or possibly one per thread, since the Random class isn't thread safe). So, you need to move the creation of the Random object outside of the loop, and organise your code so you reuse the same Random object to generate the numbers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top