1

I have an 1 dimensional array with 24 elements inside it. I want to loop through all of them in random order. I know how to loop them from 1 - 24, but how to randomly select one and then another and so on until all elements have been selected?

I'm using a VB-like scripting language (QAWizard), so I think any idea or VB code will be enough.

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
Leo Wang
  • 11
  • 2
  • I *think* you are looking for a shuffle. Looping in random order wont assure that one comes up only once (which I assume is essential if "QA" implies a quiz of some sort). – Ňɏssa Pøngjǣrdenlarp Jul 08 '16 at 15:15
  • 2
    @Plutonix I think the QA in QA Wizard stands for Quality Assurance. It seems to be an aid for testing software. It doesn't seem to be a .Net language, so I'm not convinced that the OP's assertion that "any idea or VB code will be enough" is correct.. – Blackwood Jul 08 '16 at 15:29
  • @Blackwood Could be - there are a lot of quiz game questions that get posted from time to time. Not a lot of context given. OP: If thats the case, a proper shuffle might be better. Easier to document/assure about un biased results. – Ňɏssa Pøngjǣrdenlarp Jul 08 '16 at 15:33
  • Create a list of the indexes, shuffle it, iterate through it to index into your array. Your original list is untouched - it might be expensive to shuffle that. – Andrew Morton Jul 08 '16 at 18:49
  • Thanks for the reply,@plutonix, @Blackwood, @Andrew Morton. The QA (Quality Assurance ) Wizard is a auto test tool, for software test purpose, it is a scripting language similar to VB, not a quiz game lol, my fault. Problem is that it doesn`t have lots of build-in functions like shuffle, it supports basic loop, condition, JSON, So, I don`t know how to do if you just give me a "Shuffle", if I can see some logic then I will write some built-in function and transfer it to the language. So can you please be more specific about that shuffle? Thanks ,@plutonix, @Blackwood, @Andrew Mor – Leo Wang Jul 11 '16 at 13:28

2 Answers2

2

You can use LINQ's OrderBy and Random.Next to order it randomly:

Private Shared _rnd As New Random

Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
    Dim arr As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    For Each value In arr.OrderBy(Function(x) _rnd.Next())
        Debug.WriteLine(value)
    Next

End Sub
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • 1
    `Random` might be better than `Guid.NewGuid` but OP has to remember to not use a local variable. Your `Shared` approach is fine until the app starts to be multithreaded. Then you need something like [this](http://stackoverflow.com/a/19271062/284240) ;-) – Tim Schmelter Jul 08 '16 at 15:31
  • Thanks for the reply @TimSchmeiter, can you please elaborate the part: "(Function(x)_rnd.next())" ? The QA(quality assurance) Wizard, doesn`t have the build-in function like that, I need to write it myself. so I`d appreciate it if you can help with that thx – Leo Wang Jul 11 '16 at 13:36
1

You could use LINQ:

Dim randomOrder = From item In yourArray Order By Guid.NewGuid()

For Each item In randomOrder

Next

The Guid trick is not the best way to "randomize" a collection, have a look at a shuffle approach like this: https://stackoverflow.com/a/7513502/284240

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939