Thursday, November 18, 2010

Random Numbers

Today is one of those days where I am putting together a bunch of sample reports. The reports contain fictitious data and serve as a starting point for management when they are trying to figure out what they want in a report. I may start with a bar chart in the sample report and then management will decide they want a line graph instead. Figuring out what management wants before I spend a lot of time fine tuning the report saves a lot of headache in the end.

My reports are all database driven and so it is important to populate the database with realistic-looking numbers. One of the tools I use is an existing report that I have printed and hanging on the wall of my office. When that doesn't work, I resort to random numbers. I just hate trying to make up numbers on my own.

Creating a random number with a computer is seemingly not a trivial task. My son discovered this with one of his programming assignments recently. They are using an algorithm provided by the professor, but it has one major drawback: the numbers aren't so random. If you call the function once, you will get a specific number. Then close the program, rerun it, and you will get the same number. In order to get a truly random number, you need to "seed" the algorithm with a true random number.

It may not seem like a serious problem until you truly need a random value. This is what was happening with my younger son in his ski races last year. He seemed to get stuck with the same bib number when start order for his group was supposed to be random. The software used to determine random start order was putting the same kids at the front of the group and the same kids at the back of the pack. For the kids lucky enough to be at the front of the pack, they had better snow conditions. My son, on the other hand, got relegated to the back of the pack and had to deal with ruts up to his armpits.

As with most seemingly complex problems, there is an elegant solution to having a computer generate random numbers: read the milliseconds from the computer's clock. When I explained that to my son working on the programming assignment, he couldn't believe there was such an easy solution. Now I just need to pass the information on to that bonehead who wrote the ski racing software.

No comments:

Post a Comment