Browse > Home / Code / Random Football Picks in Groovy

| Subcribe via RSS

Random Football Picks in Groovy

September 25th, 2009 Posted in Code

My friend runs an informal football picks contest every week and I thought of a way to participate in a very geeky way, a program that would make my picks for me. I decided it would be funny to write a program that would randomly make my picks for me since everybody else spends an inordinate amount of time thinking over their picks. Plus, I don’t pay attention to football so my picks would have been essentially random in any case.

The contest is simple; pick who you think is going to win in all the games. The person with the most correct picks wins. You also guess how many points the next Monday Night Football game is going to have in total so if there’s a tie, whoever is closest to that number wins.

Here’s the code for my program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def matchups = [1 : ['Atlanta','Carolina'],          2 : ['Detroit','Minnesota'],
 3 : ['Green Bay','Cincinnati'],      4 : ['Jacksonville','Arizona'],
 5 : ['Kansas City','Oakland'],       6 : ['New York(NYJ)','New England'],
 7 : ['Philadelphia','New Orleans'],  8 : ['Tennessee','Houston'],
 9 : ['Washington','St. Louis'],     10 : ['Buffalo','Tampa Bay'],
 11 : ['San Francisco','Seattle'],   12 : ['Chicago','Pittsburgh'],
 13 : ['Denver','Cleveland'],        14 : ['San Diego','Baltimore'],
 15 : ['Dallas','New York(NYG)'],    16 : ['Miami','Indianapolis']]
 
def rand = new Random(System.currentTimeMillis() + Runtime.runtime.freeMemory())
matchups.each() { key, value -> println("${value[rand.nextInt(2)]}") }
 
def score1 = rand.nextInt(5) * 7;
def score2 = rand.nextInt(5) * 3;
println("${score1} + ${score2} = ${score1 + score2}")

I’m a Groovy newbie, one of the reasons I wanted to use the language for this little thing, so I don’t know if this is the best way of doing this but this turned out to be pretty quick and easy. The hash/array data structure I used for the matchups variable makes it very easy to pick the various winners with the one-liner on 11. I went with a recommendation I found for super-extra randomness on 10 just make sure.

In the end, my random picks weren’t all that accurate. I think I had 5 right out of 16. There’s randomness for you, so unreliable. :) I also learned that when a random number generator makes your picks, you can’t take the blame for making bad picks but you also can’t take credit for the ones you got right.

Feel free to steal this code if you want to make your own picks. You have to put in all the matchups for the week but I tried to format the code so I could make the list in UltraEdit using its column-editing mode and just drop the teams in. If you have a more Groovy-ish way I could have done the matchups I’d love to hear it in the comments.

Related Articles:

You should follow me on Twitter here!


One Response to “Random Football Picks in Groovy”

  1. john Says:

    if I wanted the same program with one difference
    i only wanted to it to pick five of the games,
    what would you do.

    so 13 games, with the spreads, but only pick 5 games.

    thanks,
    john


Leave a Reply