0

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
Anax
  • 9,122
  • 5
  • 34
  • 68
  • `result = tweleveToneRow.OrderBy(Function(r) myRng.Next()).ToArray`. Make your `Random` object global so you use the same one over and over. I'd give a name *other than* the Type name, such as `myRng` in the sample – Ňɏssa Pøngjǣrdenlarp Dec 17 '15 at 16:27
  • Google "vb.net random shuffle", lots and lots of hits. – Hans Passant Dec 17 '15 at 16:32
  • The OP is not clear as to whether the algorithm should sample with or without replacement. Sampling without replacement is the same thing as generating a permutation. The results is limited to 12 items. Sampling with replacement can results in a sequenced of any desired length. – JerryM Dec 17 '15 at 16:38
  • 1
    Possible duplicate of [Best way to randomize an array with .NET](http://stackoverflow.com/questions/108819/best-way-to-randomize-an-array-with-net) – Anax Dec 17 '15 at 16:39
  • @Plutonix While that is a easy and fast way to implement a shuffle, it doesn't really produce good results. See this link for some of the reasons: https://stackoverflow.com/questions/1287567/is-using-random-and-orderby-a-good-shuffle-algorithm – Bradley Uffner Dec 17 '15 at 16:40
  • I am well aware of its limitations. But it is also short, simple to implement and usually a good-enough-shuffle for casual apps (ie this context). Any performance issues with a 12 element array will be minimal. @BradleyUffner – Ňɏssa Pøngjǣrdenlarp Dec 17 '15 at 16:44
  • @Plutonix thanks for the help, as I'm still a newb to programming, I don't understand what: Function(r) is in your solution. Can you please explain a bit further? Thank you – Aperion Project Dec 18 '15 at 03:38
  • http://stackoverflow.com/a/34074111/1070452 – Ňɏssa Pøngjǣrdenlarp Dec 18 '15 at 20:10
  • I've tried code from Plutonix and @anax and my output is always: System.string0 any advice on what I'm doing wrong? – Aperion Project Dec 19 '15 at 06:32
  • thats because `tweleveToneRow` is a string array, if you want to display whats **in** the array, use a loop or `lblToneRow.Text = String.Join(", ", tweleveToneRow)` – Ňɏssa Pøngjǣrdenlarp Dec 19 '15 at 22:04
  • @Plutonix Thanks, I should have remembered about string.join, thanks for all your help, I have a functioning tone row generator now! – Aperion Project Dec 20 '15 at 19:18

0 Answers0