ICS2O: Random Numbers What are Random Numbers? For our purposes, a random number is a number that is: a. Drawn from a pre-defined range b. Hard to predict ahead of time c. No more or less likely to be chosen than any other number. What are Random Numbers For? In computer games, you often need to generate random numbers to decide what to do next. - What is the outcome of a die roll? What card is coming next from the deck? These questions require random numbers. But game playing is not the only use for random numbers. They are also used in: - Cryptography, Statistical analysis, Sampling in surveys, Random Numbers in .NET The .NET Framework provides a class named Random that you can use in C# to generate random numbers. First you create an object from the Random class with a statement such as this (example below names the Random object lottery) Random lottery = new Random(); This causes an object called lottery of type Random. This object can now be used to create as many random numbers as you would like. Once you have your object created, use the Next() method to get an random integer. int randomNumber = lottery.Next(); This will give you a number between 0 and 2,147,483,647. int randomNumber = lottery.Next(1,50); The first argument is included in the possible result, but the second argument is not included. So the above statement will generate random integers from 1-49 inclusive. This could be used to simulate a Lotto 649 game where each number could be any integer value from 1 and 49 (inclusive). Questions 1. Write an expression to simulate rolling a single six-sided die. 2. Write the code to simulate the rolling of a pair of six-sided dice. Exercises 1. Write a console program that asks a person’s name, and then 70% of the time says that the name they typed is their favourite, and 30% of the time says they hate that name. 2. Write a console program that simulates rolling two dice, first for the user, and then for the computer. Then announce who won: the user, the computer, or say it was a tie.