0
Randomise()
Dim questions(14) as string 
Dim picked questions(4) as string 
Dim rand as new random

Questions(0) = "?"
Questions(1) = "?"
...
Questions(14) = "?" 

For counter = 0 to 4
    Pickedquestions(counter) = questions(rand.next(0,15))
Next 

I have a set of questions and I wanted to pick 5 questions from this set. However my method means that some picked questions are the same which I want to avoid. I was advised to use a sorting algorithm to randomly sort the array and then pick the first 5. The problem is I'm a beginner and I don't understand how to incorporate it into my code. Any help or other suggestions will be appreciated, thank you.

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
JackR
  • 1
  • actually you want to shuffle the array and use the first N of them. Google for Shuffle and/or Fisher-Yates, [this answer](http://stackoverflow.com/a/26532939/1070452) uses that method – Ňɏssa Pøngjǣrdenlarp Dec 20 '14 at 20:19
  • You can also copy the questions to a temp List(Of String) and randomly select them from that; removing them from the temp List as you go. Also, Randomize() does nothing for you here as that is for use with the legacy Rnd() function. You're using the new Random() class which is seeded with the current time; change it from `Dim` to `Static`. – Idle_Mind Dec 20 '14 at 20:22

2 Answers2

1

This is a VB.Net version of Jon Skeet's Shuffle() from here:

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Static rnd As New Random

        Dim questions(14) As String
        ' ...

        Dim PickedQuestions = questions.Shuffle(rnd).Take(4).ToArray()
        For i As Integer = 0 To PickedQuestions.Length - 1
            Debug.Print("PickedQuestions(" & i & ") = " & PickedQuestions(i))
        Next
    End Sub

End Class

Module Module1

    ' VB.Net Conversion of Jon Skeet's Shuffle() from: https://stackoverflow.com/a/1287572/2330053
    <System.Runtime.CompilerServices.Extension> _
    Public Iterator Function Shuffle(Of T)(source As IEnumerable(Of T), rng As Random) As IEnumerable(Of T)
        Dim elements As T() = source.ToArray()
        For i As Integer = elements.Length - 1 To 0 Step -1
            Dim swapIndex As Integer = rng.Next(i + 1)
            Yield elements(swapIndex)
            elements(swapIndex) = elements(i)
        Next
    End Function

End Module

Okay...here is something a little more "simple":

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Static rnd As New Random

    Dim questions(14) As String
    For i As Integer = 0 To questions.Count - 1
        questions(i) = "Question " & i
    Next

    Dim tmpQuestions As New List(Of String)(questions)
    Dim PickedQuestions As New List(Of String)
    For i As Integer = 1 To 4
        Dim index As Integer = rnd.Next(tmpQuestions.Count)
        PickedQuestions.Add(tmpQuestions(index))
        tmpQuestions.RemoveAt(index)
    Next

    For i As Integer = 0 To PickedQuestions.Count - 1
        Debug.Print(i & ": " & PickedQuestions(i))
    Next
End Sub
Community
  • 1
  • 1
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

Shuffle the questions array and get the first four items. This is pretty easy to do with just a little Linq:

Imports System
Imports System.Linq

...

Dim questions(14) as string 

...

Dim rnd As New Random
Dim PickedQuestions = questions.OrderBy(Function() rnd.Next).Take(4).ToArray()

While simple, and fairly straight forward, as Joel points out, this is not really the best approach (but probably sufficient for your purposes). For a better sorting algorithm, see John Skeet's answer here.

Community
  • 1
  • 1
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • ordering by random.Next only sort of works. It's probably good enough for this issue, but in the general case, it introduces a bias into the results. – Joel Coehoorn Dec 20 '14 at 20:27
  • @JoelCoehoorn True, but if I understand it, that bias only appears when two calls to `randome.Next` returns the same value. Since the range is 0 - 2 billion, the bias is going to be vanishingly small for picking 4 items out of a set of 14. OP seems like he's just learning and I didn't want to add to his confusion. Still, I've updated my answer with a little more info for completeness. – p.s.w.g Dec 20 '14 at 20:36
  • I understand this part. What I don't understand is the is the shuffling. – JackR Dec 21 '14 at 14:26