SitePoint Sponsor

# Thread: Help with this Pseudo Random Algorithm

1. ## 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.

Please help me how this can be achieved.

Thanks
Zeeshan Hashmi

2. 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.

3. thanks for your ideas !

I will use and will let you know the results !

4. No, the idea do not works.

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

Thanks
Zeeshan

5. It does "work", although maybe I musunderstand what you want, or maybe you implemented it wrong.

6. 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.

7. 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.

8. hummmm I like the idea of Impressions,

Thanks joebert !

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•