ASP/.NET Web Hosting
* 3 Months FREE *
CLICK HERE!
 CoverYourASP --> Contest --> Picking the winner!

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
74 active users
1800 visitors today
1846 pages today
(only part of today)
ASP.NET Showcase
ASP.NET Graph and Chart Component with FREE developer version

ASP.NET Shopping Cart and E-commerce Control with FREE trial download

ASP.NET Web Hosting with free month and setup

$15 Domain Name Registration from .DNR.
As seen on these sites
Special thanks go to the sites that support me by publishing my articles.
See more

CoverYourASP
Copyright © 1999-2008 James Shaw.
All rights reserved.

ASP.NET Blog
ASPRSS
ShawThing

Now open source with SourceForge!

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...

A. Total members with points = 5090
B. Total points (pool) = 7102
C. Random number (0-1) = 0.7763900473478036
D. Position in pool (B*C) = 5513
E. The winner is "Frank" (email hidden) with 1 points.

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
Step B: How many points this month?

DBInitConnection ( );

var sWhichMembers = 'nPoints>0 AND MemberLevel<4 AND Confirmed=True';

DBGetRecords ( 'SELECT Count(*) AS TotalMembers, Sum(nPoints) AS TotalPoints FROM Members WHERE ' + sWhichMembers );

var nTotalMembers = oRecordSet ( 0 ) - 0;
var nTotalPoints = oRecordSet ( 1 ) - 0;

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!

var fRandom = Math.random ();

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.

var nPosition = Math.floor ( nTotalPoints * fRandom );

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.

DBGetRecords ( 'SELECT nPoints,MemberID FROM Members WHERE ' + sWhichMembers );

var nPoints;

while ( !oRecordSet.EOF )
{
   nPoints = oRecordSet ( 0 ) - 0;
   
   if ( nPosition < nPoints )
      break;

   nPosition -= nPoints;

   oRecordSet.moveNext ( );
}

var nMemberID = oRecordSet ( 1 ) - 0;

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.

Featured sponsor
My favorite resources

Selling products online - read how James does it now


CoverYourASP Mugs, T-shirts, caps - even Boxer shorts...
I don't make a penny from these, but they're a lot of fun! Don't you need a new mouse mat?


See my source code
wherever you see this icon...

You can also download the entire site source code for FREE!


I share my content

Supporting ASPRSS

Do you need a quick and easy way to link to my articles? All the information you need is published with ASPRSS...


Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!


New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!