|
Random Order Array
Let's say you have a list of quotes or any other list and you would like to display them in a random order
whenever anyone access your page.
This code will take your list of quotes, descriptions, or whatever
and display them in a random order, dispaying them all without repeating any of them.
This code also demonsrates how to display an array in random order and different ways to use arrays.
Here is the ASP code for this script:
<% 'This is the list of stuff Dim Quote(20) Quote(0) = "Music should strike fire from the heart of man, " & _ "and bring tears from the eyes of woman. " & _ "<em>~ Ludwig Van Beethoven</em>" Quote(1) = ""Sucking all the marrow out of life" " & _ "doesn't mean choking on the bone. " & _ "There's a time for daring and there is a time for caution, " & _ "and a wise man understands which is called for. " & _ "<em>~ John Keating - "Dead Poets Society"</em>" Quote(2) = "I'm not bad, I'm just drawn that way. " & _ "<em>~ Jessica Rabbit - "Who Framed Roger Rabbit"</em>" Quote(3) = "No matter what anybody tells you, " & _ "words and ideas can change the world. " & _ "<em>~ John Keating - "Dead Poets Society"</em>" Quote(4) = "Narf! Zort! Poit! Gat! " & _ "<em>~ Pinky - "Pinky and the Brain"</em>" Quote(5) = "Am I the only one that makes cofee around here?! " & _ "<em>~ Brain - "Pinky and the Brain"</em>" Quote(6) = "I don't know the key to success, " &_ "but the key to failure is trying to please everybody." & _ "<em>~ Bill Cosby</em>" Quote(7) = "The key here, I think, is to not think of death as an end. " & _ "But, but, think of it more as a very effective way of cutting down on your expenses. " & _ "<em>~ Boris Grushenko - "Love and Death"</em>" Quote(8) = "Gentlemen. You can't fight in here. " & _ "This is the War Room! " & _ "<em>~ President Merkin Muffley - "Dr. Strangelove"</em>" Quote(9) = "Smile, it enhances your face value. " & _ "<em>~ Truvy - "Steel Magnolias"</em>" Quote(10) = "Truth is like a blanket that always leaves your feet cold. " & _ "You push it, stretch it, it'll never be enough. " & _ "Kick at it, beat it, it'll never cover any of us. " & _ "From the moment we enter crying, to the moment we leave dying, " & _ "it'll just cover your face as you wail and cry and scream. " & _ "<em>~ Todd - "Dead Poets Society"</em>" Quote(11) = "Champions aren't made in the gyms. " & _ "Champions are made from something they have deep inside them " & _ "-- a desire, a dream, a vision " & _ "<em>~ Muhammad Ali</em>" Quote(12) = "Patriotism is not short, frenzied outbursts of emotion, " & _ "but the tranquil and steady dedication of a lifetime. " & _ "<em>~ Adlai Stevenson</em>" Quote(13) = "Who is General Failure and why is he reading my hard disk? " & _ "<em>~ Steven Wright</em>" Quote(14) = "All I really need is love, " & _ "but a little chocolate now and then doesn't hurt! " & _ "<em>~ Lucy - "Peanuts"</em>" Quote(15) = "Someone sent me a postcard picture of the earth. " & _ "On the back it said, "Wish you were here." " & _ "<em>~ Steven Wright</em>" Quote(16) = "If a word in the dictionary were misspelled, " & _ "how would we know? " & _ "<em>~ Steven Wright</em>" Quote(17) = "A friend is someone with whom you dare to be yourself. " & _ "<em>~ Frank Crane </em>" Quote(18) = "Imagination is more important than knowledge. " & _ "<em>~ Albert Einstein </em>" Quote(19) = "When you come to a fork in the road, take it. " & _ "<em>~ Yogi Bera</em>" 'Quote list is the variable that will hold the quotes QuoteList = "" 'NumList is the variable that will hold the random numbers 'we will use it to avoid duplicates. 'All the numbers in the list will begin with and end with a | 'so we can keep them separated. NumList = "|" 'Turn on Randomization Randomize 'We have 20 quotes listed above, so we will will run the script 20 times For x = 0 to 19 'get random number RndQuote = Int(Rnd * 20) 'If the random number in our list is already there, 'we will keep picking numbers until 'we get one that is unique Do Until Instr(Cstr(NumList),Cstr("|" & RndQuote & "|")) = 0 RndQuote = Int(Rnd * 20) Loop 'Complile list of numbers NumList = NumList & RndQuote & "|" 'Compile list of quotes QuoteList = QuoteList & Quote(RndQuote) & "|" Next 'Trim last | from our quote list QuoteList = Left(QuoteList,Len(QuoteList)-1) 'Create an array and display our list of quotes. QuoteArr = Split(QuoteList,"|") For x = 0 to UBound(QuoteArr) Response.Write "<div class=" & chr(34) & "breg" & chr(34) & " style='padding:5px;'>" & _ "<span class=" & chr(34) & "bull" & chr(34) & "></span> " & _ QuoteArr(x) & _ "</div>" Next %>
Here are the results of the above code:
Who is General Failure and why is he reading my hard disk? ~ Steven Wright When you come to a fork in the road, take it. ~ Yogi Bera Someone sent me a postcard picture of the earth. On the back it said, "Wish you were here." ~ Steven Wright Music should strike fire from the heart of man, and bring tears from the eyes of woman. ~ Ludwig Van Beethoven If a word in the dictionary were misspelled, how would we know? ~ Steven Wright I'm not bad, I'm just drawn that way. ~ Jessica Rabbit - "Who Framed Roger Rabbit" I don't know the key to success, but the key to failure is trying to please everybody.~ Bill Cosby Am I the only one that makes cofee around here?! ~ Brain - "Pinky and the Brain" "Sucking all the marrow out of life" doesn't mean choking on the bone. There's a time for daring and there is a time for caution, and a wise man understands which is called for. ~ John Keating - "Dead Poets Society" Imagination is more important than knowledge. ~ Albert Einstein A friend is someone with whom you dare to be yourself. ~ Frank Crane Smile, it enhances your face value. ~ Truvy - "Steel Magnolias" Truth is like a blanket that always leaves your feet cold. You push it, stretch it, it'll never be enough. Kick at it, beat it, it'll never cover any of us. From the moment we enter crying, to the moment we leave dying, it'll just cover your face as you wail and cry and scream. ~ Todd - "Dead Poets Society" Gentlemen. You can't fight in here. This is the War Room! ~ President Merkin Muffley - "Dr. Strangelove" All I really need is love, but a little chocolate now and then doesn't hurt! ~ Lucy - "Peanuts" No matter what anybody tells you, words and ideas can change the world. ~ John Keating - "Dead Poets Society" Patriotism is not short, frenzied outbursts of emotion, but the tranquil and steady dedication of a lifetime. ~ Adlai Stevenson Champions aren't made in the gyms. Champions are made from something they have deep inside them -- a desire, a dream, a vision ~ Muhammad Ali Narf! Zort! Poit! Gat! ~ Pinky - "Pinky and the Brain" The key here, I think, is to not think of death as an end. But, but, think of it more as a very effective way of cutting down on your expenses. ~ Boris Grushenko - "Love and Death"
|