|
-
Oct 3rd, 2004, 06:15 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Randoming Unique Numbers
Hi guyz... One quick help, ummm I want to have a set of random numbers from 0 - 69. For example I have an array Set(70) and Set(0) will be 45, Set(1) will be 32, Set(2) will be 10, Set(3) will be 65, and so on.. What I need is to fill the entire Set() with numbers from 1 to 70 all randomly positioned, and no repeating numbers. Got it?
I tried this one with my code, It worked pretty well for 0 - 19, but when I was trying 0 - 69 im getting an endless loop with my code
VB Code:
Public Sub Randomize()
Dim aCtr As Long, bCtr As Long, nowIndex As Long, pastIndex (TOTAL) As Long, duplicate As Boolean
duplicate = True
For aCtr = 0 To (TOTAL - 1)
pastIndex(aCtr) = -1
Next
For aCtr = 0 To TOTAL_CARDS - 1
Do While duplicate = True
nowIndex = Round(Rnd() * 20)
For bCtr = 0 To TOTAL - 1
If pastIndex(bCtr) <> nowIndex Then
duplicate = False
Else
duplicate = True
Exit For
End If
Next
Loop
For bCtr = 0 To TOTAL - 1
If pastIndex(bCtr) = -1 Then
pastIndex(bCtr) = nowIndex
Exit For
End If
Next
duplicate = True
Next
End Sub
what could be wrong with my code? Have I over looped? or its just not the way its suppose to be. I havent done this randomizing exercise before..
Need Help... Thanks for all the help....Love yah guyz
Last edited by charmedcharmer; Oct 4th, 2004 at 05:38 AM.
C++ Programming is overwhelming.
Dont let it overwhelm you or you'll fall into the oblivion of its perfection
-
Oct 3rd, 2004, 06:55 PM
#2
Here is one method.
(Note the pre loading of the Array with 70 numbers ensurs NO dups!)
VB Code:
Option Explicit
' ***************************************************************
'
' Generate 70 Random numbers (no repeats)
'
' ***************************************************************
Private Sub Form_Load()
'----------------------------------------------------------------------------
' Uses an Array of 70 numbers that are subsiqently mixed randomly
'----------------------------------------------------------------------------
Dim intArr(69) As Integer
Dim strBuff As String
Dim intIdx As Integer, intRnd As Integer, intBuff As Integer, intUppBound As Integer
intUppBound = UBound(intArr)
' Load the Number Array with 70 numbers
For intIdx = 0 To intUppBound
intArr(intIdx) = intIdx + 1
Next
Randomize
' Randomly swap Array values (some swaps may be repeated)
For intIdx = 0 To intUppBound
'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
intRnd = Int((intUppBound - 0 + 1) * Rnd + 0)
intBuff = intArr(intIdx)
intArr(intIdx) = intArr(intRnd)
intArr(intRnd) = intBuff
Next
For intIdx = 0 To intUppBound
strBuff = strBuff & intArr(intIdx) & " "
Next
MsgBox strBuff
End Sub
Bruce.
Last edited by Bruce Fox; Oct 3rd, 2004 at 07:01 PM.
-
Oct 3rd, 2004, 07:18 PM
#3
Hyperactive Member
here:
put this in the form:
VB Code:
Option Explicit
Dim lenOA As Integer
Private Sub Form_Activate()
'prints out all resaults on your form
Dim i As Long
For i = 0 To lenOA - 1
Me.Print setP(i)
Next
End Sub
Private Sub Form_Load()
Randomize
Dim alrd, i, y As Long
lenOA = 70 'set this to define array size
'array is called setP and is defined in module
Call DefineL(lenOA) 'defines array's size
Call DefineL2(lenOA)
Call DefineL3(lenOA)
For y = 0 To lenOA - 1
'fill new array with the numbers left
alrd = 0
For i = 0 To lenOA - 1
If setp2(i) = False Then
setP3(alrd) = i
alrd = alrd + 1
End If
Next
alrd = Int(Rnd * alrd)
setP(y) = setP3(alrd)
setp2(setP3(alrd)) = True
Next
'********************
'final resaults are all in setP()
'********************
End Sub
and put the following in a module:
VB Code:
Public setP(), setP3() As Long
Public setp2() As Boolean
Public Function DefineL(lenOarray As Integer)
ReDim setP(lenOarray - 1)
End Function
Public Function DefineL2(lenOarray As Integer)
ReDim setp2(lenOarray - 1)
End Function
Public Function DefineL3(lenOarray As Integer)
ReDim setP3(lenOarray - 1)
End Function
hope it helps
-
Oct 3rd, 2004, 09:03 PM
#4
You could also use a collection to do this.
VB Code:
Private Sub Command1_Click()
Dim col As Collection
Dim i As Integer
Dim x As Integer
Dim intRand(69) As Integer
'Create a collection containing all the possible numbers
Set col = New Collection
For i = 0 To 69
col.Add i
Next i
Randomize
For i = 0 To 69
'Grab a random index from the collection
x = Int(Rnd() * col.Count) + 1
'Add the item from the collection to an array
intRand(i) = col(x)
'Remove the item from the collection so it can't be used again
col.Remove x
Next i
Set col = Nothing
'Display the results
For i = 0 To 69
Debug.Print intRand(i)
Next i
End Sub
-
Oct 4th, 2004, 05:35 AM
#5
Thread Starter
Addicted Member
thanks.. i didnt thought of that.. silly me.... maybe its stupid me...
thanks.. May the force be with yah guyz... ummm One person wrote something like this in vbforums: "When we encounter some problem in coding, we're stuck, then we go to vbforums, then somebody answers, then afterwards we'll realize its just a minor problem not worth asking over the forum cause we could have done it ourselves....." ehehehehehe
THANKS THANKS THANKS
C++ Programming is overwhelming.
Dont let it overwhelm you or you'll fall into the oblivion of its perfection
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
|