Forum Moderators: open

Message Too Old, No Replies

Non-Random Controlled Choice

         

rogerd

6:30 pm on Jan 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Let's say I have ten text blocks I'd like to display on pages throughout a large ASP site, one at a time. I don't really care which block goes on which page, but I'd like a more or less even distribution. I don't want them changing randomly, though, i.e., if revisits a page I don't want them to see a new text block every time.

I already created a little routine that changes the block by the day of the week - I store seven blocks in an array and choose the one for the appropriate day. Instead, though, I'd like to use the semi-random approach outlined above.

My first thought is to choose some unchanging file property and base the choice on that.

Any ideas on the most efficient way to do this?

killroy

6:46 pm on Jan 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What I do is, I calculate the checksum of the URL, feed that as seed into the random number generator (or write my own if the given one doesn't let me) and then just randomly select a block.

This ensures, no matter how many things you pick randomly, you'll always get the same ones for the same url.

SN

rogerd

11:21 pm on Jan 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



What function do you use to calculate the checksum, Killroy? I've hunted around, and the checksum calculators I'm seeing look kind of bulky. In this case, precision isn't too critical, as long as the inserted file is consistent by page.

Xoc

1:23 am on Jan 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is an asp function that I wrote to generate the checksum for any string:


Function CheckSum(strText)
Dim iabyte
Dim lngHash

lngHash = 4444 'random seed
For iabyte = 1 To Len(strText)
lngHash = CLng(lngHash And &H3FFFFF) * 33 + Asc(Mid(strText, iabyte, 1))
Next
CheckSum = lngHash
End Function

Then I'd just take the result from that, mod the number of pages you have to pick from, say you have 14 pages to pick from:


lngPage = CheckSum(Request.ServerVariables("PATH_INFO")) mod 14

lngPage will then have a value between 0 and 13, that is entirely fixed, but essentially random, as long as you don't change the local path of the page. You can use that as part of the filename or a lookup into an array to get the filename of the file you want to insert.

rogerd

1:38 am on Jan 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Thanks, Xoc... we'll give it a try!