Join Peggy Fisher for an in-depth discussion in this video Using predefined functions, part of Learning Java (2015).
- The Java API or application programming interface is a collection of software packages that programmers can use for graphics, user interface, networking, sound, database, math, and much more. It contains many methods that have already been written and tested. In order to use these specific methods in our program we have to import the necessary libraries by adding them to our program. Let's start by importing the random class.
I've created a random numbers project. On line 7, I'm going to import the random class. The random class is part of the Java.util library. By combining these libraries and are now into loops we can start to write program simulations. How about if we simulate rolling two dice one hundred times and count how many times we roll doubles? In this program we will use a random number generator to help with our simulation. We'll need to count how many times we roll doubles.
So, let's start by adding some variables. I'll start by adding an integer variable to represent the value for die1. I'll have another variable for die2 and finally, a counter that specifies how many times I get doubles. I'm gonna start the countDoubles variable out at 0. Since I haven't started yet, I don't have any doubles. Next, I need to create an instance of the random class. Similar to what we did with the scanner class I'm gonna type in the name of the class which is Random.
I'm gonna give it a variable name. I'm gonna use rand to represent my random number generator, is equal to a new Random a () and a ;. I now have an object, rand, that I can use to generate random numbers. When we wanna specify a range of possible numbers we specify one more than the upper bounds and we get numbers from 0 to that value. Since we don't want to include 0 in our case because a die starts with the number 1 and ends with the number 6, we're going to change our program to include a random number generator that has an upper balance of 6 which will give us 0 to 5 and then we're gonna add 1 so we can get the values 1 to 6.
We decided to simulate the die by rolling them one hundred times. So, remember, what kind of loop do we use when we know how many times you wanna execute the code? I hope you said a for loop. So, let's go ahead and do a for loop. So, for, now I need a variable that's gonna be my control variable so I'll do int i = 0. In order to keep going a hundred times my conditions says as long as i is < 100.
The last part the for loop syntax is the update. So, I wanna add 1 to i every time. So, I do i++. This will execute this loop from 0 to 99 which was 100 times. Now, I indicate the block of the code that should be executed with curly brackets. Inside my for loop, I'm going to declare die1 and die2 to be a random number between 1 and 6. So, let's do die 1 first is equal to a call to the random number generator.nextInt 'cause I wanna make sure that I get an integer.
I'm gonna put the number 6 in parentheses. This'll give me the numbers 0 to 5 but then if I add 1 to each number I now get a range of numbers between 1 and 6. Let's do die2 the same way. Now that I have two random numbers I want to compare them so we're gonna use a decision statement which is an if statement and we'll compare and we're gonna say if dot he value of die1 is equal to remember to compare two values you have to use the equals equals.
So, if die1 equals equals die2 meaning that we roll the same value then I am going to add one to my counter variable. countDoubles++. ++ is an abbreviation for adding 1 to the value of that variable. If the two die are not equal, I don't wanna do anything so I'm just gonna go back up to the top of my loop. Okay, when I'm all done I wanna print out the number of times that I got doubles. So, outside the loop, so if I click here I can see the start and stop of my for loop.
It looks like line 29 is outside of my for loop 'cause I only want to print the total of number of doubles one time. I'm gonna do System.out.printlin and I'll say I rolled. I wanna dd the number of doubles that I rolled, countDoubles+ again and I'll put the word doubles. So, when I run the program it'll replace countDoubles with a number and it'll say I rolled fifty doubles or whatever the number is.
Okay, I think we're ready to test this. I'm gonna go ahead and run it. It looks like the first time through I got 19 doubles. The way that we have it set up we can run it again without changing anything and because it randomly generates each die I should get a different number. This time I got 13. Now, I got 18. As you can see, the number of doubles ranges somewhere between 13 and 18 so far but the numbers could change if I keep testing. The random number class is included in the Java.util.random library.
For more information, refer to the Oracle Java doc for detailed information on all the predefined methods available for the random class.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs