|
-
Jun 23rd, 2010, 11:02 AM
#11
Re: Random Unique Lists of 12 Numbers
 Originally Posted by anhn
OK! That code produce one random sequence of 12 different numbers.
How can you know that an output sequence is not the same as one of the previous ones?
What the OP wants is all the output sequences must be different to each other.
I missed those lines while reading 
I worked out some code:
Code:
Option Explicit
Private Sub Command1_Click()
'~~~ We are going to produce 4 lines of unique sequence
Dim OutputArray(11) As Byte '~~~ This will hold the numbers generated using the function
Dim strUni(4) As String '~~~ Unicode value(of byte array) of all 4 lines
Dim strOutput As String '~~~ Final result
Dim strTemp As String
Dim n As Long
Dim i As Long
Do While n < 4 '~~~ We are generating 4 lines of sequence
If n > 0 Then
strTemp = StrConv(OutputArray, vbUnicode) '~~~ Getting the unicode string for the generated array values
i = 0
Do While i < 4 '~~~ We are going to check this unicode value with others.
If strTemp = strUni(i) Then '~~~ If it is matched, then the array is shuffled and the checking is restarted.
ShuffleArray OutputArray '~~~ Shuffle the array
strTemp = StrConv(OutputArray, vbUnicode) '~~~ Unicode value of the shuffled array
i = 0 '~~~ Start the checking (loop) from the beginning
Else
i = i + 1
End If
Loop
End If
strUni(n) = mySequence(OutputArray) '~~~ Storing the unicode string of the present byte array values
'~~~ Creating that line of sequence of the present byte array values
For i = 0 To 11
strOutput = strOutput & " " & OutputArray(i)
Next
strOutput = strOutput & vbCrLf '~~~ We are going to start a new line
n = n + 1
Loop
MsgBox strOutput '~~~ Final result
End Sub
Private Function mySequence(ByRef myOrder() As Byte) As String
Randomize
Dim myArray(1 To 90) As Boolean
Dim num As Byte
Dim counter As Integer
Do While counter < 12 '~~~ 12 values (for each line)
num = Int(Rnd * 90 + 1) '~~~ Finding a random number from 1 to 90
If myArray(num) = False Then '~~~ Check if it is already selected
myArray(num) = True '~~~ Marking it as selected number
myOrder(counter) = num '~~~ Storing the number
counter = counter + 1
End If
Loop
mySequence = StrConv(myOrder, vbUnicode) '~~~ Returning the unicode value of this byte array
End Function
' Knuth shuffle (very fast). Thanks to "Ellis Dee" for this shuffle code
Public Function ShuffleArray(pvarArray As Variant)
Dim i As Long
Dim iMin As Long
Dim iMax As Long
Dim lngReplace As Long
Dim varSwap As Variant
iMin = LBound(pvarArray)
iMax = UBound(pvarArray)
For i = iMax To iMin + 1 Step -1
lngReplace = Int((i - iMin + 1) * Rnd + iMin)
varSwap = pvarArray(i)
pvarArray(i) = pvarArray(lngReplace)
pvarArray(lngReplace) = varSwap
Next
End Function
I don't know how efficient it is (I don't think it is efficient at all )....
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|