Help with this Pseudo Random Algorithm

• Apr 10, 2009, 21:46
zeeshanhashmi
Help with this Pseudo Random Algorithm
Hello

I have a situation, where I added some links in the database with their IMPORTANCE from 1 to 10. (where 10 is HIGHEST).

Now, i have an Ad Rotator that picks the link randomly and present that.
BUT what I want is that if a link is given the HIGHER importance that should be presented more than once (on page loads). So For example :

Suppose there are 5 AD Links are stored. Ad no. 2 is given a higher importance.

User A visits PAGE and the PAGE shows AD No. 1
User B visits PAGE and the PAGE shows AD No. 2
User C visits PAGE and the PAGE shows AD No. 3
User D visits PAGE and the PAGE shows AD No. 4
User E visits PAGE and the PAGE shows AD No. 2
User F visits PAGE and the PAGE shows AD No. 5
User G visits PAGE and the PAGE shows AD No. 2
------------ a cycle of random links complete --- now it will start again
User H visits PAGE and the PAGE shows AD No. 2
User I visits PAGE and the PAGE shows AD No. 5
User J visits PAGE and the PAGE shows AD No. 1
User K visits PAGE and the PAGE shows AD No. 2
User L visits PAGE and the PAGE shows AD No. 3
User M visits PAGE and the PAGE shows AD No. 2

you can see that in picking the RANDOM links, AD No. 2 was shown multiple times.

Thanks
Zeeshan Hashmi
• Apr 10, 2009, 22:08
crmalibu
Code:

```SELECT ad   FROM ads ORDER     BY importance * rand()  LIMIT 1```
This could possibly yield the same ad multiple times in a row.

This isn't the most efficient query though if the table were to get large. A really easy way around that(if needed) would be to fake randomness. Just generate the list of ad id's ahead of time, give them a sequence, and cycle through them.
• Apr 11, 2009, 07:04
zeeshanhashmi
thanks for your ideas !

I will use and will let you know the results !
• Apr 18, 2009, 23:48
zeeshanhashmi
No, the idea do not works.

Some one, please guide me how this can be done!

Thanks
Zeeshan
• Apr 19, 2009, 07:58
crmalibu
It does "work", although maybe I musunderstand what you want, or maybe you implemented it wrong.
• Apr 19, 2009, 08:33
goodmast3r
higher importance? How much higher. 3 out of 10? 4 out of 10? If you have 5 ads, and ads 2 have higher importance, then randomize from 1-10. if it shows 6-10, then show ad number 2. So ad no 2 will show more than the other ads.
• Apr 19, 2009, 11:24
joebert
Add an "impressions" column to the table.
Add an "importance" column to the table.

Select a random row where "impressions" > 0.

If there are no rows with impressions > 0 then update the table so that "impressions" column = the value of the rows "importance" column and select again.
Code:

`UPDATE table SET impressions = importance`
Decrement the impression column for the returned row.

A row with a higher importance is going to therefore have more impressions.
• Apr 21, 2009, 21:53
zeeshanhashmi
hummmm I like the idea of Impressions,

Thanks joebert !