Forum Moderators: coopster

Message Too Old, No Replies

How do I achieve consistent random results?

a bit of an oxymoron

         

MrSpeed

1:20 pm on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a php/mysql site that for the sake of discussion has 1000 pages.

I'd like to sprinkle in 5 random links on each page to other pages on the site. However I want the same 5 every time the pages loads. Think of it a a mini site map on each page.

I have a couple of ideas and I'm not sure which way to go.

--I could just get the next 5 items from the table and wrap if I'm near the end of the table.

--I could use a straight forward rand and cache the 5 links and using them every time a page loads. But I figured there must be a better way than than going to the hard drive and wasting space on cache files.

--I could seed the rand with the id of the current row number of the database.

srand($row["id"])
$linkid=rand()
for ($i=1; $i<=5; $i++)
{
getlinkforitem($linkid)
$linkid=$linkid+1
}

or would this be better....

for ($i=1; $i<=5; $i++)
{
srand($row["id"]+$i)
$linkid=rand()
getlinkforitem($linkid)
}

--Then I thought hmm....what if I try something like converting the url to a md5 hash to seed the rand function. I would have to strip out letters from the hash. I could then use the resulting integer to seed the rand function.

Am I going down the right path? Any thoughts or ideas?

killroy

1:30 pm on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hehe, you are. Was going to suggest it but saw u figured it out already ;) You can convert the hash to a pure number and mod it.

SN

claus

1:49 pm on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> I'd like to sprinkle in 5 random links on each page

Imho, it would be better with 5 relevant non-random links ;)

(and sorry for just posting this, i thought the thread had to do with randomization in general)

MrSpeed

2:17 pm on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Imho, it would be better with 5 relevant non-random links ;)

I was expecting someone would raise this issue :)
In theory all the links would be related since the site is about widgets. I'll be linking to brown widgets, furry widgets, classic widgets.

And heck maybe some links won't be related. In my mind that's ok. Maybe my visitors are tired of widgets. Here are some links to celebrity gossip, online games and other mindless stuff.

MrSpeed

2:31 pm on Apr 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can convert the hash to a pure number and mod it.

When you say "mod it" do you mean the modulas operator?
Is there a simple way to convert to a pure number? I was thinking preg_replace.