Monday, March 25, 2013

posting by chance

I had a delightful day with my girlfriend yesterday. =)

But just before I went to bed, I realized I hadn't written a single line of code all day, and that's bad luck. =)

It was already late and I after searching for ten minutes or so (man, it's hard to find a website with real simple exercises) I chose a trivial problem: write a program that simulates rolling a par of dice (I wanted something really simple, just to write my bit of code for the day and not upset the Coding Spirits).

I started writing a test, something like:

def test_roll_dice
  die = Die.new
  result = die.roll
  # assert... wait, what?
end

What would be my assertion? That the result had to be between 1 and 6? But if I write a roll() method which returns numbers between 1 and 7, the test would pass 85% of the time. I thought about it for a while, and I really don't have any clue of how to test random stuff. I hope Kent covers that in his book...

The really fun thing is that something trivial like this led me to a meaningful question, something interesting that I will surely look into later.

I guess programming is like a box of chocolates... =)

No comments:

Post a Comment