I'm trying to randomize a number in VB.NET 3 times. And each time I randomize a number it should be different from the other two numbers.
For example I have 3 integers. Int1,Int2 and Int3. I will randomize Int1 between 1-10 , and then I will randomize Int2 between 1-10 however the value shouldn't be equal to the value I randomized in Int1 and the same goes for Int3 it shouldn't equal to Int1 and Int2.
I have figured out how to randomize a number, this is the code I'm using:
Dim RndNumber As Random
Dim num,num2 As Integer
RndNumber = New Random
num = RndNumber.Next(1, 11)
num2 = RndNumber.Next(1, 11)
Now I'm stuck on how I make num2 randomize a number between 1-10 that is not equals to num.
I appreciate any help, thanks.
In all the examples,
RNG
is a random number generator created from the NETRandom
class:Linq
If you only need two or three, you could loop until the current pick is not in the result set. But this is even simpler using some extension methods:
This starts with all numbers between 1 and 10, puts them in random order, takes the first 3 and stores them in the
nums
array. I used the multiline form, breaking after the.
s to illustrate the steps.Just change the range, size/count and
Take()
element as needed. For instance, for something like a lottery with 5 unique numbers 1-69 (condensed form):Since the Powerball can be a repeat of the first numbers, it comes from its own pool. Since we only want one, we dont need an array, just the
First()
.Manual
It is good to know the logic for these things, so this shows a manual version. This does it differently, by picking and actually checking random values:
Rather than loose vars, this uses a list to hold the picks. This makes it easy to see if the current pick has already been selected. For more than just a few values, use a
HashSet(Of Int32)
rather than a List for performance.Random Pairs
To create a random sets of numbers with 2 of each, such as for a matching game, just double up the base pool of values then put them in random order:
For a manual method you would have to check the count of each value in the loop.
'Use up' Picks
One more variation is when you need a pool of randoms used periodically, but you don't know how many you will need in advance. Examples would be the balls for a BINGO game or a deck of cards.
Rather than a global indexer pointing to the last slot used (or next slot to use), a
Stack(Of T)
(or aQueue
) will "use up" values as you need them:This starts basically the same with 100 integers, randomized and stored in an array, but there is no
Take(n)
because we want them all. They values are then stored in a stack collection. Using it:When you
Pop
a value it is removed from the collection automatically. If you use a lot of values from the shoe, you will want to check the count to make sure it is not empty.After drawing 3 values, the shoe has only 97 values remaining.
Random Notes
In all cases your
Random
generator should be a form level object which you create once. Never create them in a loop or you will likely get the same value over and over.The
OrderBy(Function(r) RNG.Next)
method of randomizing is usually good enough for casual use, but it is inefficient. If you will be randomizing large sets and/or using it frequently you should consider using a proper shuffle such as the Fisher-Yates shuffle shown here.