jeudi 17 décembre 2015

In Visual Basic, how do I randomize an array of strings?

I have an array filled with strings that I want to randomize the order of and display that order in a label every time a button is pushed. The array is filled with the names of the 12 pitches in Western music.

I've considered and attempted a few different methods, and can't solve a problem in the two main ways I've tried doing this:

1) With or without using an array, assigning/converting randomized integers 1-12 to a string value (specifically: 1=C, 2=C#, 3=D, 4=Eb, 5=E, 6=F, 7=F#, 8=G, 9=Ab, 10=A, 11=Bb, 12=B). I know how to display random numbers 1-12, but I want to display their corresponding note names.

2) Using an array, I haven't figured out how to randomize the elements. I've tried, the last two lines of my code are intentionally wrong, just so everyone can see what I'm attempting to do:

Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click
    Dim tweleveToneRow() As String = {"C", "D", "E", "F", "G",
        "A", "B", "C#", "Eb", "F#", "Ab", "Bb"}
    Dim random As New Random()
    tweleveToneRow = random.Next(13)
    lblToneRow.Text = tweleveToneRow
End Sub




Aucun commentaire:

Enregistrer un commentaire