advertisement

Random Number Generation Within a Console application project, within the Main method, create a Rarndom object, and use it to generate several random numbers using the Next method, like so: static void Main() { Random nums = new Random( ); // Console.WriteLine("Next number: Console.WriteLine("Next number: Console.WriteLine("Next number: } seed value based on time {0}", nums.Next() ); {0}", nums.Next() ); {0}", nums.Next() ); You'll notice that the values produced by nums.Next tend to be quite large. In fact, the range of values that this method may produce ranges from 0, up to and including 2,147,483,646 (which is Int32.MaxValue – 1). As it turns out, there's another version of the Next method, which you can use to produce values within a specified range, which I would recommend using whenever you're programming on the .Net platform, or any other platform that also has this functionality. So what happens if you're using a platform (such as C++) that doesn't have this functionality? What if you wanted to randomly generate numbers between 0 & 5, similar to rolling a die would? It turns out that given the parameter-less Next() method, you can still generate numbers within the range of a die, using the modulus ('remainder') operator. For this exercise, you should get together with a partner, and figure out how to generate numbers randomly within the range 0 through 5, using the parameter-less Next() method, and using the modulus operator. Next, write some simple text code: put this code in a loop, and have it generate many random numbers. Each time, check that the randomly generated number isn't less than 0, or greater than 5, in order to double-check that your code (probably) works. Once you've gotten that done, see if you can generate numbers in the following ranges. Each time, write some test code to double-check your work: A. 1 through 6 B. 2 through 9 C. -10 through +10 D. only the EVEN numbers in the range 0 through 8 E. only the ODD numbers in the range -11 through 3