RSS Feed

Yankee Swap Number Generator

3

December 17, 2012 by Mike Hillwig

Like many companies, we have a lunch around this time of year where we exchange gifts. In my office, we do what’s called a Yankee Swap. This always means drawing numbers from a hat.

Somehow, this has become problematic in my office and nobody wants to be responsible managing the numbers. As I’ve said to many an intern in the past, “I can replace you with a SQL script.” And that’s just what I did here.  Now, this is hardly an example of good code. I’m breaking the golden rule by putting a clustered index on a GUID. And worse yet, I’m sorting on a substring of a GUID. But I will submit that it’s still more random than pulling numbers from a hat, which allows people to cheat.

[SQL]CREATE TABLE #ys_users

(PERSON VARCHAR(50),
ID UNIQUEIDENTIFIER PRIMARY KEY CLUSTERED NOT NULL DEFAULT NEWID())

INSERT #ys_users (PERSON) VALUES (‘Tina’)
INSERT #ys_users (PERSON) VALUES (‘Chris’)
INSERT #ys_users (PERSON) VALUES (‘Jim’)
INSERT #ys_users (PERSON) VALUES (‘Mike’)
INSERT #ys_users (PERSON) VALUES (‘Cheryl’)
INSERT #ys_users (PERSON) VALUES (‘Tim’)

SELECT PERSON FROM #ys_users ORDER BY LEFT(ID,1) ASC, LEFT(ID,2) DESC, LEFT(ID,3) ASC

DROP TABLE #ys_users

[/SQL]

You can run this script over and over, and you will get a different result each time.  The results will look something like this.


Search

Pages