popup Weight Algorithm

1. Senior Member
Join Date
Dec 1969
Posts
162

## popup Weight Algorithm

I have a problem.<BR><BR>1. I to set up a system of popups. It can be 1- whatever.<BR><BR>2. The popup needs to have a weight. Ex: <BR><BR>Popup 1 has a weight of 20%<BR>Popup 2 has a weight of 70%<BR>Popup 3 has a weight of 10%<BR>Popup 4 has a weight of 40%<BR>And so on.<BR><BR>Depending on their weight thats how they popup.<BR>How can this be done?<BR>

2. Senior Member
Join Date
Dec 1969
Posts
549

## Eh?

Could you explain some more please.

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## The easy way...

First of all, how can you possibly have a total of 140%???<BR><BR>What you mean is you have WEIGHTED NUMBERS, not weighted percents.<BR><BR>So the easy way to do this is to create an array with as many elements in it as the sum of all the numbers. <BR><BR>And then you put each popup name into the array as many times as its weight. So if POPUP1 has a weight of 2, put it&#039;s name (or, better, just number) into the array 2 times. If POPUP2 has a weight of 7, put its number/name into the array 7 times, etc.<BR><BR>And then you simply choose a random element of the array!<BR><BR>You can store the array in an Application variable, so you don&#039;t have to keep creating it all the time.<BR><BR>If you were driving this off a DB, you could do something like this:<BR><BR>&#060;SCRIPT RUNAT=SERVER&#062;<BR>Sub Application_onStart( )<BR> Set conn = Server.CreateObject("ADODB.Connection")<BR> conn.Open ...<BR> Set RS1 = conn.Execute("SELECT SUM(weight) FROM popupsTable")<BR> Dim popups( )<BR> popupCount = RS1(0)<BR> RS1.Close<BR><BR> Set RS2 = conn.Execute("SELECT popupNumber, weight FROM popupsTable")<BR><BR> curwt = 0<BR> ReDim Popups( popupCount-1 )<BR> Do Until RS2.EOF<BR> num = RS2("popupNumber")<BR> wt = RS2("weight")<BR> For w = 1 To wt<BR> Popups(curwt) = num<BR> curwt = curwt + 1<BR> Next<BR> RS2.MoveNext<BR> Loop<BR> Application("Popups") = popups &#039; save the array<BR> Application("PopupCount") = popupCount<BR>End Sub<BR>&#060;/SCRIPT&#062;<BR><BR>And then to get a weighted random popup in an ASP page:<BR><BR>&#060;%<BR>Randomize<BR><BR>popup = Application("Popups")( INT( RND * Application("popupCount") ) )<BR>%&#062;<BR><BR><BR>

#### Posting Permissions

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