|
-
Nov 29th, 2006, 10:04 PM
#1
Thread Starter
Hyperactive Member
Randomizing a list? (please see other details)
Lets assume I have a list of 100 words that are in alphabetical order.
I want to click a button called "Randomize"
This button will mix the words so that the list is no longer in alphabetical order. Each time I click randomize it will mix up the list.
Does anyone have any code or know how to do this?\
Thank You!
8 gigs/ram (hey why not)
300 gig HD x2
Windows XP 64
-
Nov 29th, 2006, 11:36 PM
#2
Fanatic Member
Re: Randomizing a list? (please see other details)
My first pass...
Dim strWords(100) as string
'List1 contains the 100 words
'Create random number generator 0 to 100
'
for a=0 to list1.listcount
bb:
b=int(Rnd(1*10)*100)
if b > 100 then goto bb
aa:
if len(strWords(b)) > 0 then b=b+1 : goto aa
strWords(b)=list1.list(a)
next
Well, something like that....my fast coding example
-
Nov 29th, 2006, 11:59 PM
#3
Re: Randomizing a list? (please see other details)
VB Code:
Option Explicit
Private sWords() As String
Private Sub Form_Load()
Dim cnt As Long
Randomize
ReDim sWords(99)
For cnt = 0 To 99
sWords(cnt) = "word " & cnt
Next
Call Shuffle(100)
Text1.Text = Join(sWords, vbCrLf)
End Sub
Private Sub Shuffle(ByVal nCount As Long) 'nCount +1 is number of elements affected by shift
Dim idxA As Long
Dim idxB As Long
Dim sTmp As String
Dim cnt As Long
'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
idxA = Int((99 - 0 + 1) * Rnd + 0)
sTmp = sWords(idxA) 'init circular shift
For cnt = 1 To nCount 'do intermediate shifts
Do
idxB = Int((99 - 0 + 1) * Rnd + 0)
Loop Until idxA <> idxB 'so you dont get indices to same element
sWords(idxA) = sWords(idxB)
idxA = idxB
Next
sWords(idxB) = sTmp 'finish circular shift
End Sub
Last edited by leinad31; Nov 30th, 2006 at 05:31 AM.
-
Nov 30th, 2006, 12:03 AM
#4
Hyperactive Member
Re: Randomizing a list? (please see other details)
VB Code:
Private Sub Form_Load()
Dim myWords(5) As String
myWords(0) = "ABC"
myWords(1) = "DEF"
myWords(2) = "GHI"
myWords(3) = "JKL"
myWords(4) = "MNO"
Randomize
Dim i As Integer
Dim tmpWord As String, rndNum As Integer
For i = 0 To UBound(myWords) - 1
rndNum = CInt(Rnd * (UBound(myWords) - 1))
tmpWord = myWords(rndNum)
myWords(rndNum) = myWords(i)
myWords(i) = tmpWord
Next
For i = 0 To UBound(myWords) - 1
Debug.Print myWords(i)
Next
End Sub
-
Dec 1st, 2006, 11:01 AM
#5
Addicted Member
Re: Randomizing a list? (please see other details)
Beleive it or not I do that sort (no pun intended) of thing all the time.
Personally I do it by looping through the array and swapping my "current position" with a random one somewhere else in the list.
Something like this:
For i=1 to 100
r=random number between 1 and 100
temp=arrayvalue[i]
arrayvalue[i]=arrayvalue[r]
arrayvalue[r]=temp
next i
-
Dec 1st, 2006, 12:20 PM
#6
Re: Randomizing a list? (please see other details)
Here's how I'd do it.
VB Code:
Dim colRandom As New Collection
Dim strValues(99) As String
Dim intIndex As Integer
Randomize
On Error Resume Next
Do Until colRandom.Count = 100
colRandom.Add Int(100 * Rnd)
Loop
For intIndex = 0 To 99
strValues(intIndex) = List1.List(intIndex)
Next
List1.Clear
For intIndex = 1 To 100
List1.AddItem strValues(colRandom(intIndex))
Next
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
|