![]() | Pepper Spray & Stun Gun Specials! KEEP THEM SAFE. | |
| CoverYourASP --> Contest --> Picking the winner! | ||
| Let's pick a winner!This page is used by me to determine the winner of the contest every month. The choice is entirely random, so what you see gives no indication of who the real winner will be!I have tested this code over 10,000,000 times to test that the random number generator (a member of the JavaScript Math object) gives a fair spread over the entire range. It passed the test perfectly. So, without further ado, let's roll those dice...
How is it done?Let me walk you through the very simple code, one step at a time, starting with...Step A: How many members are there, and
First, I open a connection to my database using a function defined in my utils/database.asp Server Side Include. Then I initialize a variable, sWhichMembers, with the qualifications that a member must satisfy to take part. Members must have accumulated some points (obviously), be less than administrator level (I don't want to win) and be confirmed members. DBGetRecords( ) then queries the database for the number of members that meet these criteria, and the total number of points they have. I place the results in two more variables for later use... Step C: Give me a random number!
This couldn't be much easier - I just call a member of the JavaScript Math object. It is a surprisingly good random number generator - I analyzed it and the spread was very consistent over small and large data sets. Step D: Where in the "pool" of points is that? Having got the total number of points, and a random number between 0 and 1, I multiply them together to find the "point" that won this contest.
I use another Math method, floor( ), to round down to the nearest whole number. The result is that I have picked a single point out of the entire point "pool". All I have to do then is... Step E: Finding the winning member The last part of this process is to walk through the members and find who has the winning point.
The call to DBGetRecords fills the recordset with a complete list of the qualifying members, which I then loop through. In the loop I subtract each members points from the winning position until I reach the correct member. I exit the loop and end up with the MemberID of the winner! I actually notify each winner personally rather than have an email sent automatically - it's more satisfying that way. =;-) The only thing left is to reset all the member points to zero and mark this contest as over by setting the relevant Awarded flag in the Contest table. All that is taken care of with the ResetContest.asp utility. ![]() |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||