Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2006-06-26 08:18:54

mwillse
Member
From: brooklyn, ny
Registered: 2006-06-26
Posts: 83
Website

sortby random really sortdir

Unlike other sortby options, rand() doesn’t affect which articles are selected. Sortby random only affects how those items are displayed.

The power of a random selection is not in mixing up a list’s display, but in randomly selecting from the depths of your archive.

for example:

<txp:article_custom category="design" sortby="rand()" limit="5" />

If you use this in a sidebar throughout your site, your visitor will see the same 5 items on everypage, just shuffled in a different order. I understand other people might use it for something different, but I’m not sure what that would be.

If it grabbed every design article, randomly sorted them, and then grabbed five, your visitor would have ongoing sample of random articles.

It seems like it should first randomly sort the entire list of articles that meet your request AND THEN select the ones set by your limit.

any suggests on how to work around this?

Offline

#2 2006-06-26 08:48:04

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: sortby random really sortdir

Unlike other sortby options, rand() doesn’t affect which articles are selected.

Yes it does.

It seems like it should first randomly sort the entire list of articles that meet your request AND THEN select the ones set by your limit.

That’s exactly what is already done, that’s how SQL works.

If it grabbed every design article, randomly sorted them, and then grabbed five, your visitor would have ongoing sample of random articles.

It would also be an extremely bad approach.

Related info:

http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html

ORDER BY RAND combined with LIMIT is useful for selecting a random sample from a set of rows… RAND is not meant to be a perfect random generator, but instead is a fast way to generate ad hoc random numbers which is portable between platforms for the same MySQL version.

http://www.openwin.org/mike/index.php/archives/2003/12/mysql-function-of-the-week-rand

Where RAND can be very useful is for choosing rows at random.

What you’re probably experiencing is a lack of a large number to pick from; the larger the list is, the more “random” the selection can be. Another possibility is a bug in your installed version of MySQL.

Offline

#3 2006-06-26 15:42:55

mwillse
Member
From: brooklyn, ny
Registered: 2006-06-26
Posts: 83
Website

Re: sortby random really sortdir

It would also be an extremely bad approach.

what would be a bad approach? i’m not suggesting that this be the sole means to provide a visitor access to previous articles. it’s an example for the purpose of discussion. for my application, it’ll be in addition to the usual methods as a means to feature historic items deemed important.

if what i’ve described is exactly how SQL works as you say, then something is amiss. what you describe should happen, isn’t. while selecting 3 items out of my test pool of 20 might be a small sample size, it seems unlikely that RAND would grab the same 3 a dozen times in a row.

Offline

#4 2006-06-26 22:19:12

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: sortby random really sortdir

mwillse, I’m using <txp:article_custom limit="1" section="quotes" sortby="rand()" />. If what you said is true, it would always show the same article. It doesn’t, on every pageload another article is displayed.

Offline

#5 2006-06-27 00:25:52

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: sortby random really sortdir

while selecting 3 items out of my test pool of 20 might be a small sample size, it seems unlikely that RAND would grab the same 3 a dozen times in a row.

It is unlikely, though of course technically possible. Are you certain that your browser isn’t caching your page?

As a variation on Els’ comment, what happens when you use:

<txp:article_custom category="design" sortby="rand()" limit="1" />

?

Offline

Board footer

Powered by FluxBB