Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unique random numbers, NOT repeated? 1

Status
Not open for further replies.
Jul 13, 2001
180
US
Hello, I have a dymanic scroller that pulls "headlines" from a database. What I want to accomplish is have the 4 or 5 head lines have a different stylesheet out of 4 possibilites, but never be repeated twice in a row. How do I get it to choose a random number out of 4, but never repeated?
i.e. 2, 3, 1, 4 not 2, 2, 1, 4

Here is my code.

<%dim upperlimit
dim lowerlimit
upperlimit = 4
lowerlimit = 1
randomize
<% r2="asae"&Int((upperlimit - lowerlimit + 1)*Rnd() + lowerlimit)%>
<% Response.Write "messages["&i&"]=""<br><a class="&"'"&r2&"'"&"href='press_full.asp?ID="&trim(current("ID"))&"&page=pressreleases'>"&trim(current("Title"))&"</a></font>"""
%>

Thank you.
 
Something like this ?
Code:
Dim arrUsed(): ReDim arrUsed(upperlimit)
nbElem=upperlimit-lowerlimit+1
Dim arr2Use(): ReDim arr2Use(nbElem)
Randomize
For i = lowerlimit To upperlimit
  arrUsed(i)=0
Next
i=1
While i <= nbElem
  j=Int(nbElem*Rnd()+lowerlimit)
  If arrUsed(j)=0 Then
    arr2Use(i)=j
    arrUsed(j)=1
    i=i+1
  End If
Wend
The unique random numbers are in arr2Use(1 to nbElem)

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top