[Chicago] How could I make this better?
clydeforrester at gmail.com
Wed Mar 9 16:51:06 CET 2011
Malcolm Newsome wrote:
> Hey all. I wrote this simple program for my children today. I’m
> wondering if there was a more efficient alternative to the way I wrote it.
Interesting program. I have 3 concerns:
1. Your decision tree.
Your major possibilities include:
or D. None of the above.
Condition D should never happen. You should either consider the
possibilities better, or throw a diagnostic fail if D happens.
2. Range of numbers.
The range of integers from 0 through 100 covers 101 integers. This is a
common boundary condition problem. It relates to my next point.
3. Integer vs floating point.
If you want integers, and you ask for a random floating point number,
you have some figuring to do. For a range of integers from 1 to 100, you
would get a floating point random in the range of 0 to 100, and then
what? Recast as an integer? How? If you round to nearest, then 0 and 100
have a .5% chance each. If you round up, that could work better.
But it might be much easier to use something like random.randint(1,100).
So, thanks for contributing. Great programming exercise for you. Great
code review exercise for the rest of us.
More information about the Chicago