Generate list of unique random integer values?

J

juvi

Hello,

I have a code in vb.net to generate unique integer values from 1 to 52 to a
list, but it generates every time the same values in the same order. How to
correct this ? thx juvi

Dim shuffle(52) As Integer
Dim rnd As New Random(52)

Sub Generate()
Dim x, mix, temp As Integer

For x = 1 To 52 'assign sequential values to the array

shuffle(x) = x

Next x

For x = 1 To 52 'swap array positions randomly

mix = rnd.Next(1, 53)

temp = shuffle(mix)

shuffle(mix) = shuffle(x)

shuffle(x) = temp

Next x
End Sub
 
H

Herfried K. Wagner [MVP]

juvi said:
I have a code in vb.net to generate unique integer values from 1 to 52 to
a
list, but it generates every time the same values in the same order. How
to
correct this ? thx juvi

Dim shuffle(52) As Integer
Dim rnd As New Random(52)

=> 'Dim rnd As New Random()'.
 
R

rowe_newsgroups

Hello,

I have a code in vb.net to generate unique integer values from 1 to 52 toa
list, but it generates every time the same values in the same order. How to
correct this ? thx juvi

Dim shuffle(52) As Integer
Dim rnd As New Random(52)

Sub Generate()
Dim x, mix, temp As Integer

For x = 1 To 52 'assign sequential values to the array

shuffle(x) = x

Next x

For x = 1 To 52 'swap array positions randomly

mix = rnd.Next(1, 53)

temp = shuffle(mix)

shuffle(mix) = shuffle(x)

shuffle(x) = temp

Next x
End Sub

You need to change the seed for the random, I normally use the empty
constructor.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
 
A

Andrew Morton

juvi said:
I have a code in vb.net to generate unique integer values from 1 to
52 to a list, but it generates every time the same values in the same
order. How to correct this ? thx juvi

Randomize()
Dim cards As New List(Of Integer)
For i As Integer = 1 To 52
cards.Insert(Int(i * Rnd()), i)
Next

Andrew
 
A

AMercer

I agree with the other responders about
Dim rnd As New Random()

Also, I assume that in
mix = rnd.Next(1, 53)
53 is just a typo - should be 52.

Also, your algorithm generates biased shufflings - the permutations that it
generates are not equi-probable. They are nearly so, but I assume you want
the shufflings to be as random as possible. The algorithm you want is
something like the following to permute elements a(1) to a(52) inclusive:

For i = 52 To 2 Step -1
j = rn.Next(1, i)
k = a(i) : a(i) = a(j) : a(j) = k
Next

This algorithm generates one of the 52! (52 factorial) permutations of the
deck so that the probability of the result is 1/52!. In other words, the
permutations are equi-probable. Your algorithm does something close, but it
has a bias. If you don't believe me, run a million iterations of your
shuffler and evaluate the resulting statistics. You will find that the first
element of your array, ie shuffle(1), is mapped to a new position between 1
and 52, but these positions are not equally probable. Ditto for all others.
 
A

AMercer

Regarding:
Also, I assume that in
mix = rnd.Next(1, 53)
53 is just a typo - should be 52.

My mistake, 53 is right.

Regarding:
For i = 52 To 2 Step -1
j = rn.Next(1, i)
k = a(i) : a(i) = a(j) : a(j) = k
Next

My mistake again, should be
j = rnd.Next(1, i+1)

Sorry about these errors - they came from my mindset about my own random
number utilities where both lower and upper bounds are inclusive. With
rnd.Next, the upper bound is exclusive.

My observation about the bias of the OP's shuffler still stands.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top