Question

I have a program that I have built for personal use that will randomly select a movie from a set of checked items in a listbox. One of the features I programmed into this application is the ability to generate a 'Tiered Result'. What I mean by a tiered result is that an algorithm should first randomly select three movies, then randomly select two of those, then finally randomly select a final movie from those two.

To help illustrate what I mean:

Movie List : Shriek, Shriek 2, Shriek 3, Monsters Inc, Bambi, Bambi 2

Tier 1 : Shriek 3, Bambi, Bambi 2

Tier 2 : Shriek 3, Bambi

Tier 3 : Bambi

I have successfully accomplished this with the following code:

Private Sub btnPick_Click(sender As System.Object, e As System.EventArgs) Handles btnPick.Click
    If boxMovies.CheckedItems.Count <> 0 Then
        Dim rnd As New Random

        If My.Settings.Tier = True Then
            lbl1.Text = boxMovies.CheckedItems.Item(rnd.Next(boxMovies.CheckedItems.Count))
            Randomize()
            lbl2.Text = boxMovies.CheckedItems.Item(rnd.Next(boxMovies.CheckedItems.Count))
            Randomize()
            lbl3.Text = boxMovies.CheckedItems.Item(rnd.Next(boxMovies.CheckedItems.Count))

            Randomize()
            Dim stp2() As String = {lbl1.Text, lbl2.Text, lbl3.Text}
            lbl4.Text = stp2(rnd.Next(stp2.Length))
            Randomize()
            lbl5.Text = stp2(rnd.Next(stp2.Length))

            Randomize()
            Dim stp3() As String = {lbl4.Text, lbl5.Text}
            lbl6.Text = stp3(rnd.Next(stp3.Length))
        Else
            MessageBox.Show(boxMovies.CheckedItems.Item(rnd.Next(boxMovies.CheckedItems.Count)), "Movie Result", MessageBoxButtons.OK)
        End If
    Else
        MessageBox.Show("Please pick at least one movie!", "No Movies Selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End If

End Sub

The issue I am having however is that movies can be randomly generated twice in any given tier. The image below should help illustrate what I mean:

enter image description here

Notice how in Tier 2, 'Star Wars Saga' is generated twice. This ultimately results in 'Star Wars Saga' being the ensured result in Tier 3.

I was wondering if there was an easy way (feel free to improve my code, I realize it is probably inefficient, I just threw something together quickly) to resolve this. I would like results in each tier to be unique to that tier.

Was it helpful?

Solution

Imagine you have a vector V with N movies (or indexes to the movies).

For i:=0 to 2 do
   r := rand(N-i-1)            // A random integer fro 0..N-i-1 inclusive
   AddToResult( V[r] )
   V[r] := V[N-i-1]            // Override the selected element with the last one.

The idea is to select a random element from the list, and put the last element in the place of the extracted element. So the new list will have a length of N-1 and will not include the extracted element. This new list has a different order, but because we select a random one, we don't care of it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top