HEJ!
Nej, denna kod har ju ingen hantering för undantag av id som inte används, tänkte jag skriva, men det har ingen betydelse, den hoppar ju till den post som slumpas fram?
Alltså borde det fungera.
I vilket fall ville jag delge min kod jag gjorde för några år sedan för att slumpa ut i detta fall sju recept, utan dubletter.
Detta löstes genom att lägga posterna i en array som krymptes efter varje slumpning.
Ta bort ' från raderna och se hur vackert det fungerar :e
sql="SELECT id, namn FROM recept"
RS.Open sql, dbconn, 1, 2
If NOT RS.EOF Then
GRrecept = RS.GetRows
uboundarray=UBound(GRrecept,2)
antalrec=7: antalslump=0: i=0
If uboundarray+1 > antalrec Then
Do Until antalslump = 7
'for k=0 to uboundarray-i
'response.write GRrecept(0,k) & " " & GRrecept(1,k) & "<br>"
'next
Randomize
slumptal=Int(Rnd*(uboundarray+1-i)) '+1 pga array 0,1...
'response.write "slump" & slumptal & "<br>"
slumparrayrec(0,antalslump) = GRrecept(0,slumptal)
'response.write "slumparrayrec0," & i & " " & slumparrayrec(0,antalslump) & "<br>"
slumparrayrec(1,antalslump) = GRrecept(1,slumptal)
'response.write "slumparrayrec1," & i & " " & slumparrayrec(1,antalslump) & "<br>"
'flytta posterna över slumptalet nedåt
For j = slumptal To uboundarray-1-i
GRrecept(0,j) = GRrecept(0,j+1) '+1 ska ju flytta nästa tal
'response.write "GRrecept1," & j & " " & GRrecept(0,j+1) & "<br>"
GRrecept(1,j) = GRrecept(1,j+1)
'response.write "GRrecept1," & j & " " & GRrecept(1,j+1) & "<br>"
Next
'response.write "ubound före krymp " & UBound(GRrecept,2) & "<br>"
ReDim Preserve GRrecept(UBound(GRrecept, 1), UBound(GRrecept, 2) - 1)
'response.write "ubound efter krymp " & UBound(GRrecept,2) & "<br>"
i=i+1
antalslump = antalslump+1
Loop
Else
For j = 0 to uboundarray
slumparrayrec(0,j) = GRrecept(0,j)
slumparrayrec(1,j) = GRrecept(1,j)
Next
End If
For k=0 To UBound(slumparrayrec,2)
response.write "<a href=""rec.asp?id=" & slumparrayrec(0,k) & "">" & slumparrayrec(1,k) & "</a>"
Next
Else
response.write "Det finns inga recept."
End If
RED: Se http://www.webforum.nu/showthread.php?s=&postid=837740#post837740