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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with random

Status
Not open for further replies.

mrsbean

Technical User
Jul 14, 2004
203
US
I want to use a query to return a random set of data.

Code:
<cfquery name="get_fp" datasource="this_content" MAXROWS=4>
SELECT table_text.text_client_id, table_text.text_id, table_text.text_title, table_text.text_content, table_text.text_link, Rnd([text_id]) AS random
FROM table_text
WHERE (((table_text.text_client_id)=39) AND ((table_text.text_title) Like 'Featured Product%'))
ORDER BY Rnd([text_id]) desc;
</cfquery>
later in the code ...

Code:
<cfloop query = "get_fp" >
					<a href="<cfoutput>#text_link#</cfoutput>"><img src="[URL unfurl="true"]http://www.somewebsite.com/client_images/text/<cfoutput>#text_id#</cfoutput>_1.jpg"[/URL] /></a><br />
					</cfloop>

When I run this query and Access and examine the first four records, I get a different set of four every time I run it. However, my ColdFusion results are ALWAYS the same four.

What can I do differently to return a random set of "featured products"? I suspect that I run into a problem with the MAX rows in the cfquery statement, but I don't know what to do to fix it.

MrsBean

Later in my code,
 
Have you tried not caching the CFQUERY? If it works in Access but not in ColdFusion it may be getting cached.

Does Rnd() take a seed? I think it does. If you are seeding it with text_id you are probably giving it the same seed each time and getting the same results. Try generating a number based on the time or something so you can get something random, and then use that as the seed.

I'm not very familiar with Access anymore but that's the way randomizing functions usually work if they take an argument.
 
Glowball,

The [text_id] is a number. It is the autonumber field stored in a table which tracks text blocks. I tried using a portion of the time so that I would get a truly random number, but it didn't seem to make any difference.

I'll check into the cached issue, but I'm not sure I undertand it.

MrsBean
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top