
Weighted Random Numbers
Ok, I know how to create a pseudorandom number in ASP, but this time I need to weight the numbers. So, if I were to have a contest where there are four prizes.<BR><BR>1. $1<BR>2. $2<BR>3. $3<BR>4. $4<BR><BR>I obviously want to have more $1s show up than $4 dollars. Also, I want to be able to know that statisically that with 1,000 hits 1 will show up a certain percentage of the time.<BR><BR>any ideas?<BR><BR>thanks!<BR>

RE: Weighted Random Numbers
I know how to do this in JScript, and I'm sure it can be converted to VBScript:<BR><BR><%@ Language="JScript" %><BR><%<BR><BR>var aNumbers = new Array(10)<BR>aNumbers[0] = "1"<BR>aNumbers[1] = "1"<BR>aNumbers[2] = "1"<BR>aNumbers[3] = "1"<BR>aNumbers[4] = "2"<BR>aNumbers[5] = "2"<BR>aNumbers[6] = "2"<BR>aNumbers[7] = "3"<BR>aNumbers[8] = "3"<BR>aNumbers[9] = "4"<BR><BR>var now = new Date()<BR>var sec = now.getSeconds()<BR><BR>var iRandomNumber = ar[sec % 10]<BR><BR>%><BR><BR>In this array, there are ten items, so replace all instances of 10 with however many will be in your array. The variable iRandomNumber contains the final output by taking the seconds, and deciding whether it is divisible by 10, and if not, how far out it is.<BR><BR>The table above makes the probability of getting a one 4/10, the prob of getting a two 3/10, the prob of getting a three 2/10 and the prob of getting a one 1/10. You can change this by changing the number of times each number occurs in the array.

Use an array...
As with so many questions, the answer is an array.<BR><BR>There are a couple of ways to do this.<BR><BR>If the number of prizes, total, is small, then just create an array that will hold the total number of prizes, repeating prizes as need be:<BR><BR><%<BR>prizes = Array( 1,1,1,1,1,1,1,1,1,1,2,2,2,2,3,3,4 )<BR>%><BR>17 prizes, yes? And say you want the chance of *any* prize being given to an entrant to be 1 in 10,000. So first pick a random number from 0 to 99999. If it matches a prechosen number (heck, let the user choose his/her own number!), *then* you pick one of the prizes from the array, at random.<BR><BR>********************<BR><BR>Another way: If the number of prizes isn't that small (Reader's Digest?) then use an array that carries the prize number or value *and* the odds of winning:<BR><BR>prizes = Array( "A100", "B400", "C10000", "D100000" )<BR><BR>Now you choose a prize at random (0 to 3, element of the array).<BR><BR>You get the prize and the odds:<BR><BR><%<BR>temp = prizes( randomChoice )<BR>prize = Left( temp, 1 )<BR>odds = CLNG( Mid( temp, 2 ) )<BR>%><BR>and then you pick a number from 1 to the odds and, if it matches a preselected number (just using 1 isn't a bad idea), the contestant wins that prize.<BR><BR>NOW... This latter method isn't as good as the former, because with the former, when a prize is awarded you can simply replace its entry in the array with (say) 1, indicating it is gone.<BR><BR>With the latter method, you could conceivably end up awarding two Prize D's, even with only two entrants, even with the odds 1 in 100000 or worse. Not likely. But could happen.<BR><BR>So with the latter method you need a separate array to keep track of prizes awarded.<BR><BR>And then you have to decide whether, if a person *seems* to have won a prize but there are no more of that particular prize available, do you redraw for that person or just say "you lose".<BR><BR>You get to decide.<BR><BR>Neither of these are perfect simulations of pulling a slip of paper out of a goldfish bowl, though. So you might want to think how to do a better job of simulating that.<BR><BR>

Hope you don't mind...
You can initialize a JS array the same way you can a VBS array:<BR><BR>var aNumbers = new Array( 1,1,1,1,2,2,2,3,3,4 );<BR><BR>I might point out that this still doesn't serve the stated desire of having the odds of pulling prize 1 be one in a thousand.<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

Forum Rules

