Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Why isn't Global returning random?

Quote Reply
Why isn't Global returning random?
I put this global together from another Alex wrote to return random links which would not repeat. I'm trying to use it to return certain listing on static pages. Although this seems to work well there is one link always being returned 9 time out of 10. Since it's not really random enough with that one link always showing up I don't see how I can use it.

By looking at this global does anyone see why it's not returning all randomly?

sub {
my $tags = shift;
my $link_db = $DB->table('Links');
my $limit = $tags->{Random_Limit} || 4;
my ($output, $sth, $link);
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
$sth = $link_db->select ( { isValidated => 'Yes', galleries => 'Yes' });
while ($link = $sth->fetchrow_hashref) {
$output .= Links::SiteHTML::display ('gallery_table', $link);
}
return $output;
}


Called:
<%galleries1%>
<%loop galleries1_loop%>
<%endloop%>



Thanks for any help sorting this out

Jonze

Last edited by:

Jonze: Feb 19, 2003, 1:39 PM
Quote Reply
Re: [Jonze] Why isn't Global returning random? In reply to
Looking at the msql manual, you may need to have a Group By statement in there:

('Group by ID', 'ORDER BY RAND()', "LIMIT $limit");


Something like that.

Just a thought.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Why isn't Global returning random? In reply to
Thanks for the suggestion Pugdog.

By adding the 'GROUP BY ID' statement I get the same results. I searched through the mysql docs looking for anything to do with the rand function. Found this...

http://www.mysql.com/...tical_functions.html

When I add in a seed value 'ORDER BY rand(20)' I get proper randomization. The problem is it won't update results.

I've noticed that the one problem link that is always being displayed in the first loop has the lowest 'ID'. Which is also the first link added to the db.

I think Afinlr may have seen some similar problem by looking at this random global thread...

http://www.gossamer-threads.com/...orum.cgi?post=209201

Anyone know of a way to prime the rand() function with a value or shuffle up that array? I'm just pulling strawls but I really need to get something working. It's those first few loops that are showing problems.

Thank for any help or suggestions!

Crazy
Quote Reply
Re: [Jonze] Why isn't Global returning random? In reply to
How many links are in the database? If you go to Database->SQL Montitor and type:

SELECT ID FROM lsql_Links ORDER BY RAND() LIMIT 5

and run that a number of times, do you still see the same link show up?

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Why isn't Global returning random? In reply to
Thanks a lot for the reply Alex and the useful code.

I ran that sql query a bunch of time and came up with the same results....

The lowest ID link "4" came up at least 9 times out of 10 like before. Strange huh? I did finally get it working properly for the most part by seeding the RAND function with

ORDER BY RAND(NOW())

Found here:
http://www.mysql.com/..._time_functions.html

I read on several sites NOW() is good to use when seeding the random generator. Seems like it works pretty well so far.

Thanks again Alex Sly