Reputation: 19
So I am making Deal or No Deal and i'm currently trying to randomise what case holds what amount of money. At the moment my code creates the list of values and then randomises a selection which then inputs into the array. It then outputs to the console the 26 values. Now when a value from the list is selected and inputed into the array i want that value to be removed from the list. So then when the loop repeats and chooses another item from the list, and it can no longer select the one that was chosen in the previous loop.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim i As Integer
Dim values = New List(Of Integer)({0.5, 1, 2, 5, 10, 15, 25, 50, 80, 100, 500, 750, 1000, 2000, 5000, 10000, 25000, 50000, 80000, 100000, 150000, 250000, 500000, 850000, 1000000, 10000000})
Dim sselect = New Random()
Dim cases(25) As Integer
For i = 0 To cases.Length - 1
cases(i) = sselect.Next(0, values.Count)
Console.WriteLine(values(cases(i)))
Next
End Sub
I tried to use RemoveAll
and then AddressOf
to remove the duplicate values, but it doesn't seem to work.
values.RemoveAll(AddressOf cases)
Upvotes: 1
Views: 628
Reputation: 54417
There's a better way:
Dim rng As New Random
Dim cases = {0.5, 1, 2, 5, 10, 15, 25, 50, 80, 100, 500, 750, 1000, 2000, 5000, 10000, 25000, 50000, 80000, 100000, 150000, 250000, 500000, 850000, 1000000, 10000000}.OrderBy(Function(dbl) rng.NextDouble()).ToList()
Done! By the way, how are you going to store 0.5 in a List(Of Integer)
?
EDIT:
In my original code, the end result would be an IEnumerable(Of Double)
. I should have added a called to ToList
to get a List(Of Double)
, which I have now added.
The code uses an implicitly-typed array and, because there is a Double
value in there, the array is inferred to be type Double
. All the Integer
values are implicitly converted to Doubles
. If you want a List(Of Decimal)
instead of a List(Of Double)
then simply change 0.5
, which is a Double
literal, to 0.5D
, which is a Decimal
literal.
Upvotes: 0
Reputation: 4210
You can make use of the RemoveAt
while choosing the amounts which:
Removes the element at the specified index of the List.
Like this:
For i = 0 To cases.Length - 1
cases(i) = sselect.Next(0, values.Count)
Console.WriteLine(values(cases(i)))
values.RemoveAt(cases(i))
Next
Using this way will remove the used value from the list values
right on the spot, which in turn won't allow duplicate values to be chosen.
I suggest that you keep a copy of the original list before iterating through that. A fast and simple way is to create the list twice at the start.
Dim values = New List(Of Integer)({0.5, 1, 2, 5, 10, 15, 25, 50, 80, 100, 500, 750, 1000, 2000, 5000, 10000, 25000, 50000, 80000, 100000, 150000, 250000, 500000, 850000, 1000000, 10000000})
Dim original = New List(Of Integer)({0.5, 1, 2, 5, 10, 15, 25, 50, 80, 100, 500, 750, 1000, 2000, 5000, 10000, 25000, 50000, 80000, 100000, 150000, 250000, 500000, 850000, 1000000, 10000000})
This way after removing all of them from the values
list. You'll still have them in original
list. Assuming you'll reuse them later on.
Upvotes: 1