Presentation is loading. Please wait.

Presentation is loading. Please wait.

Random Data ICS2O.

Similar presentations


Presentation on theme: "Random Data ICS2O."— Presentation transcript:

1 Random Data ICS2O

2 Introduction Random data is useful in a lot of circumstances in programming. It allows us to add a sense of chaos into our programs. We see it often in video games, such as gambling games, loot dropping games, etc.

3 Setup In order to generate a random value we need to use a new library. At the top of your program write: import java.util.Random; Next we need a new variable that we will use to generate the random value. This variable is an Object of type, Random Inside your Main class, but above the main code block write the following variable definition: static Random rng = new Random(); rng is an acronym for Random Number Generator We use the static key word because we are defining the variable outside the main code block which is static. To be compatible we need to state that rng will also be static. You only need ONE rng for your entire program, even when generating many random values. You simply reuse it as necessary. Finally, you need a variable (or more) to hold the value to be generated. Its name depends on its purpose. It may be something like diceRoll or lottoNum For our example: Inside the main code block define: int lottoNum;

4 Generating a Number Part 1
The base functionality of generating random number will give us a float number between 0 and 1 Depending on your goal, this may suit your needs To generate a number from 0 to 1: float randomNum; randomNum = rng.nextFloat();

5 Generating a Number: Part 2
However, usually we want to generate a random integer within a range of values. E.g. a lotto number from 6 to 49 To do this we need a bit of setup, here is a small piece of code that will generate a random integer in the range 6 to 49: import java.util.Random;  1) import Random library public class Main { static Random rng = new Random();  2) Create Random Number Generator Object public static void main(String [] args) int lottoNum;  3) Define variable(s) to hold the random number int rangeLow = 6;  4) Create variables defining the number range int rangeHigh = 49;  5) Generate a number from 0 to 1 (e.g. 0.1), then multiply by high-low (e.g. 49-6=43) E.g. 43 * 0.1 = 4.3, Now add low. Here: = Now cast to an int  10 lottoNum = (int)((rng.nextFloat() * (rangeHigh – rangeLow)) + rangeLow); } This last line follows BEDMAS and goes in the following order: (rangeHigh – rangeLow) => (49 – 6) = 43  lottoNum = (int)((rng.nextFloat() * 43) + 6) The biggest possible number will be 43 (until we add the rangeLow at the end bringing it back to 49) rng.nextFloat() gives a random float from 0 to 1, e.g  lottoNum = (int)(( * 43) + 6) Math: ( * 43) =  (int)(( ) + 6) Add the rangeLow to bring up the possible range to that value as a minimum: ( ) = Cast: (int)( ) = 11


Download ppt "Random Data ICS2O."

Similar presentations


Ads by Google