This gives you a random index (range of 0 to [array length - 1]) that will then give you the random contents (-5 to 5). [ January 12, 2005: Message edited by: PJ Chin ]
Joined: Jul 30, 2003
Sure, but what happens when the user comes back and says, "Oh, now we need -10 to 10"? The hard-coded values method would need to be rewritten, where the general method would not. I always try to think "Reusability" when I write code, and part of that is not having any hard-coded values (except perhaps in the testing/driver program)...
To add more flexibility, you may want to write a method with the following signature:
This would return a random integer between min and max. I'll leave the implementation as the proverbial exercise for the reader. However, it shouldn't be difficult to modify Jeff's program above for this purpose.