Forum Moderators: open

Message Too Old, No Replies

Need help with a formula using ASP

kind of complicated...

         

mattglet

2:40 pm on Sep 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm trying to figure out a way to turn this idea into code:

At first, I have a site with 3 pages. I need 50% of visitors to go to page1, 25% of visitors go to page2, and 25% of visitors go to page3. Totally random, doesn't matter who you are, you just need to get bumped to one of those 3 pages.

Now the tricky part:
There will be more pages added every so often by the site owner, and the percentages will become dynamic. So theoretically, there could be 7 pages, with percentages of 40%, 10%, 10%, 10%, 10%, 10%, 10%

Using ASP & SQL Server, is there anyone with an efficent pseudo-idea?

I just need a push in the right direction.

aspdaddy

7:39 pm on Sep 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is an interesting problem. I'm sure theres a more efficient way, but off the top of my head this seems like it would work!


When adding a page :

get the fileCount of the files in the 'pages' folder
Dim an array - weightings (fileCount)
Assign the weighting percentages to each element of the array - not sure how your %ages work out here.
Read LastPageCount value from a text file - (initially set this value to 0)
if fileCount<>LastpageCount then
update the text file
insert a new row in the pageAdded table (pkDate/Time, NewNumberofPages)
loop 1 to filecount (n)
create records in a child table (pkAutonum, fkNewNumberofPages, pageNumber=n, hits=0, percentage = weightings(pageNumber) )
end loop
end

When hitting the homepage:

Read LastPageCount value from a text file
select the child records for NewNumberofPage=LastpageCount
multiply out the hits with the %ages.
write the values to an array and sort ascending
select the first elemenet - intPage
update NewNumberofPage where pageNumber=intPage set hits=++hits
Redirect to the page in the files collection index (intPage)

If the index on the files collection is unreliable, you will have to maintain a mapping of page id=page name in a database.

Good Luck!

dataguy

7:59 pm on Sep 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do this on a number of sites using a random number, usually generated from the right-most digits from the timer function. I then do a select case set my percentages. It's not sequential, but over time and with enough visitors, the percentages will be right on. I also do a server.transfer instead of a redirect so the pages all have the same URL as far as the visitor is concerned.

f00sion

5:11 am on Sep 9, 2004 (gmt 0)

10+ Year Member



When I have had to do random selection likes this based on percentages I do it like this:
assume 3 pages, pageA 70%, pageB 20%, and pageC 10%.
create an array of size 100.
fill index 0-69 with 'A'
70-89 with 'B'
and 90-99 with 'C'
Then generate a random number (i) 0-99, then looking up array[i] will give you either and a, b, or c.
This has worked for me on a few occassions when a type of load balancing is needed for whatever based on percentages, the slickest way I could think of doing it.

petehall

3:42 pm on Sep 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello!

Create a table in your SQL Server Database:

PageID INT
PageName VARCHAR
Percentage FLOAT
Total FLOAT

Fill the table with the following data:

PageID,PageName,Percentage,Total
1,Page1.asp,50,5
1,Page2.asp,25,3
1,Page3.asp,25,2

Use the following query:

SELECT PageID, PageName, Percentage, Total, Total/Percentage AS PercentageTotal FROM Pages ORDER BY PercentageTotal

Then increment the Total field of the first record in the set by 1. Run the query again and you will notice the positions change.

If you keep using this technique I think you'll keep an exact number of viewings to 50%/25%/25% although the method of displaying the pages is not random.

You could easily modify percentages and page names in the table making your solution dynamic and easy to impliment / change.

musicales

11:04 am on Sep 15, 2004 (gmt 0)

10+ Year Member



Create an application variable and add 1 to it each time the page is called, looping back to 0 when it gets up to 100.
Then simply do a select case on the application variable - if it's the first 10 go to page 1 etc.
No database calls needed then.

petehall

7:19 pm on Sep 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) What is wrong with making database calls?
2) What happens if the server / website is re-started? You then have no record...

mattglet

11:54 am on Sep 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I ended up taking the basis of petehall's idea, and added in a few sprinkles of a few other people's ideas.

I used SQL to keep track of the total number of hits, and also keep the total number of hits each page has gotten. I made a big stored procedure that (among other things) divides the page hits by the total hits, finds the percentage, and returns back the page to direct to.

Thanks for everyone's help.