Reputation: 2110
I am using Classic ASP, and have a MySQL table ("j_un2") with 6,318 random words in, the total count there will never change. The IDs in the table are gapless.
I need to generate a list of 5 random concatenated strings made up of 2 words, where the total length of the string is 8 characters long.
I already got some very useful help a few days ago via: Selecting random words from table Optimising SQL to concatenate random words
Based on the help I received, I wrote this simple function to generate 8 character random word combinations:
<%
Function f1(str)
found = "no"
do while found <> "yes"
rand1 = Int((Rnd * 6138) + 1)
rand2 = Int((Rnd * 6138) + 1)
SQL = "SELECT CONCAT(w1.fld_un, w2.fld_un) word FROM j_un2 w1 , j_un2 w2 WHERE w1.fld_id = "&rand1&" AND w2.fld_id = "&rand2&""
set pRS = oConn.Execute(SQL)
word = pRS("word")
if len(word) = str then
found = "yes"
f1 = word
end if
Loop
End Function
for i = 1 to 5
bob = f1(8)
response.write bob & "<br />"
next
%>
It works pretty quickly, runs in less than 1 second, which is great.
However, the words it generates are always in the same order - e.g.
digpills
grincost
grownjaw
jonesbin
cloudme
If I refresh the page, it generates the words in the same order. I know that there are many hundreds of words in the table which will combine to make an eight character long string, so it's not that there aren't enough words in the database.
If I do e.g. this:
for i = 1 to 5
bob = f1(8)
response.write bob & "<br />"
next
for i = 1 to 5
bob = f1(8)
response.write bob & "<br />"
next
for i = 1 to 5
bob = f1(8)
response.write bob & "<br />"
next
for i = 1 to 5
bob = f1(8)
response.write bob & "<br />"
next
Then it will generate 20 random 8 character long combinations, but again, those 20 are always in the same order.
I am not a very smart programmer - am I missing something, as I can't work out how to get a completely random list each time I refresh the page.
Upvotes: 0
Views: 970