JackR
JackR

Reputation: 1

Stopping part of an array being repeated

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.

Upvotes: 0

Views: 108

Answers (2)

Idle_Mind
Idle_Mind

Reputation: 39122

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

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 148990

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.

Upvotes: 0

Related Questions