Friday, February 29, 2008

Random Numbers

This week I was skimming over a JavaScript book and I found an interesting piece of code. In JS, Math.random() returns a random number between 0 (inclusive) and 1 (exclusive). Here is the algorithm the book suggests to get a random number between 0 and 4:

do{
    iValue = Math.random() * 10 // random number between 0-10
    iValue = Math.floor(iValue) // round to nearest integer
} while (iValue > 4)

Awesome!!

It is assuring to know that the algorithm will not only generate a random number between 0 and 4, but it will also take a random number of steps to finish. That's what I call taking random number generation to the next level.

The book is "Learning JavaScript" (O'Reilly) and the algorithm is shown on page 91.

Wednesday, February 6, 2008

Dilbert

I have recently discovered some Dilbert cartoons on YouTube. This is not the best one, but it is a nice one to start with: