Generating a Random Number4:08 with Ben Deitch
The heart of the logic behind our Fun Facts app is randomly selecting a fact. In this video we'll learn how to create and use objects of Java's Random class to generate a random number in a given range!
Back in our funfacts activity class, we've just finished setting it up so 0:00 that when we tap on our button, this OnClickListener detects that tab. 0:04 And then runs the code we see here in this onClick method. 0:09 Right now the app is pretty boring. 0:13 It just shows this one fact over and over each time we hit the button. 0:16 Let's take a look at this code and 0:20 talk about how we can change it to be more exciting. 0:22 We start with a string variable named fact, which holds the text of our fact. 0:25 But for now, it's hardcoded as, Ostriches can run faster than horses. 0:30 To make this dynamic, 0:35 we need to change the fact variable based on some sort of randomization. 0:37 Let's start by setting our fact variable 0:41 equal to an empty string by deleting everything between the quotation marks. 0:44 We have to leave our fact variable equal to something, or 0:49 else we'll get an error when we try to set the text. 0:52 Then, let's add some comments to give us an outline of what we hope to accomplish. 0:55 Let's add a line above our fact variable and say, Randomly select a fact. 0:59 Then let's add two lines below our fact variable and add a comment that says, 1:08 Update the screen with our new fact. 1:13 And that's all there really is to the heart of our app. 1:18 We randomly choose a fact, and then we display it to the user. 1:21 All right, let's start by creating a new random object named Random Generator. 1:25 Random is a class provided by Java itself that makes it easy for 1:31 us to generate all sorts of random numbers. 1:35 Let's add a new line up here and then type Random, and 1:38 call it randomGenerator, = new Random. 1:42 Then let's create a new integer named randomNumber, 1:50 to store the number we generate. 1:53 And let's set it to = randomGenerator. 1:58 And then right at the top of autocomplete is the nextInt method we want. 2:03 Now we're generating a random integer. 2:09 But how do we know what range it's going to be in? 2:11 What if it's a really high number? 2:14 We were only going to have ten facts in our app. 2:17 But for right now lets pretend we only have three facts to keep it simple. 2:19 How can we limit our randomNumber generator to just three choices? 2:24 Luckily, there's another version of the nextInt method that takes in a parameter 2:28 that tells it how many numbers to choose from. 2:33 Lets go back between the parentheses and type a 3. 2:35 Now seems like a good time to stop and 2:39 make sure we have things working like we want them to. 2:40 Instead of setting our label to the blank string stored in the fact variable, 2:43 let's quickly test our randomNumber generator by setting our fact variable 2:48 equal to our random number. 2:52 And we get an error, if we hover over it, it says Incompatible types. 2:57 The fact variable is looking for a string, a randomNumber is an int. 3:03 We need a randomNumber to be a string, an easy way to do this 3:08 is to force a conversion by adding an int and a string together. 3:11 Let's add + and then an empty string and viola, the error disappears. 3:15 The + can be used to add numbers together, but it can also add strings together. 3:22 And when it's between a number and a string, the string version wins, and 3:27 the number gets automatically converted. 3:32 Since the string we're adding to our number is empty, 3:34 this ends up just being the string representation of randomNumber. 3:37 Which is exactly what we want, all right, let's run this and see what happens. 3:41 And if we click on our button we see that our text view 3:49 gets updated with different numbers. 3:52 Cool, and it looks like the list of numbers starts at 0 instead of 1. 3:56 So we get 0, 1, 2 instead of 1, 2, and 3. 4:02
You need to sign up for Treehouse in order to download course files.Sign up