Down below is the beginning code of what I am trying to do... The goal is:
- get all records from a table (done)
- get 10 random items (done)
- if you encounter an item that has already been used, skip it (having trouble)
I tried to solve my problem by creating an array, populating it with random recordset numbers which i generate on every loop and then compare a new random recordset items with the items already in the array... i hope it's clear.
Oh yeah - the purpose: this is suppose to be a random "internal link collection" on a website.
I use ASP, VbScript (as you see below) and Access 2000.
Code begin
:::::::::::::::::::::
::::::::::::::::::::::::
Code End
As you see, i discovered how to detect an element which is alredy in the array, but i don't know how to go from there to randomize again until all the numbers are different.
Any ideas? Am I complicating things too much?
- get all records from a table (done)
- get 10 random items (done)
- if you encounter an item that has already been used, skip it (having trouble)
I tried to solve my problem by creating an array, populating it with random recordset numbers which i generate on every loop and then compare a new random recordset items with the items already in the array... i hope it's clear.
Oh yeah - the purpose: this is suppose to be a random "internal link collection" on a website.
I use ASP, VbScript (as you see below) and Access 2000.
Code begin
:::::::::::::::::::::
Code:
' dim the vars
Dim rsH
Dim cnt
Dim RndNumber
Dim rndMax
Dim arrHitre(10)
' create rs
Set rsH = Server.CreateObject("ADODB.Recordset")
' open rs
rsH.Open "SELECT Something FROM SomeTable", strConn, 3, 1
' set count = 0 for adding later
cnt = 0
' get number of rs items
rndMax = rsHitre.RecordCount
'loop until we get all ten links
Do Until cnt = 10
' get random number
Randomize
RndNumber = Int(Rnd * rndMax)
'loop for comparing rndNumber to array items
Dim iLoop
For iLoop = 0 to 10
' is there already an item with the same no.
If RndNumber = arrHitre(iLoop) And then
' found the element
Response.Write "Element exists! (RndNumber:" & RndNumber & ")<br>"
End If
Next
' add the item to array
arrHitre(cnt) = RndNumber
' go to chosen recordset
rsHitre.MoveFirst
rsHitre.Move RndNumber
' write out link
Response.Write rsH("SomeLink")
' add 1 to cnt
cnt = cnt + 1
Loop
' close and destroy rs
rsH.Close
Set rsH = Nothing
::::::::::::::::::::::::
Code End
As you see, i discovered how to detect an element which is alredy in the array, but i don't know how to go from there to randomize again until all the numbers are different.
Any ideas? Am I complicating things too much?