Generating a Random Number4:01 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 FunFactsActivity class, we've just finish setting it up so 0:00 when we tap on our button, this onClick listener detects that tab and 0:04 then runs the code we see here between the brackets. 0:09 Right now, the app is pretty boring, it just shows this one fact over and 0:13 over each time we hit the button. 0:17 Let's take a look at this code and 0:21 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:26 but for now it's hardcoded as, Ostriches can run faster than horses. 0:31 To make this dynamic, 0:36 we need to change the fact variable based on some sort of randomization. 0:37 Let's start by setting our fact variable equal to an empty string by deleting 0:42 everything between the quotation marks. 0:46 We have to leave our fact variable equal to something or 0:50 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:56 Let's add a line above our fact. 1:00 And then say Randomly select the fact. 1:04 Then let's add two lines below our fact variable and add a comment that says 1:09 Update the screen with our new fact. 1:14 And that's all that really is to the heart of our app. 1:22 We randomly choose a fact and then we display it to the user. 1:25 All right, let's start by creating a new random object named random generator. 1:29 Random is a class provided by Java itself that makes it easy for 1:34 us to generate all sorts of random numbers. 1:38 Let's add a new line up here, and then ty[e val randomGenerator, 1:41 and let's set it equal to a new random object by typing Random(), 1:47 then use Alt+Enter to import the class. 1:52 Then let's create a new val named randomNumber to store the random number 1:57 we generate. 2:00 And let's set it equal to randomGenerator.nextInt 2:02 to set it to a random integer. 2:06 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:13 We are only going to have ten facts in our app. 2:16 But for right now, let's pretend that we only have three facts to keep it simple. 2:18 How can we limit our random number generator to just the three choices? 2:23 Luckily, there's another version of the nextInt method that takes in a parameter 2:27 to tell it how many numbers to choose from. 2:32 Let's go back between the parentheses and type a 3. 2:34 Now seems like a good time to stop and 2:37 make sure we have things working like we want them to. 2:40 Instead of setting our label for 2:43 the blank string stored in the fact variable, let's quickly test our 2:44 random number generator by setting our fact variable equal to our random number. 2:48 And we get an error down here where we update the text view. 2:56 If we hover over it, it says, type mismatch. 3:00 Our TextView requires us to use a char sequence or 3:03 a string when updating the text, but we're passing in an int. 3:06 Since we're not specifying a type or a fact variable, 3:11 its type will be the same as whatever we set it equal to. 3:14 So in this case, by changing it from an emptyString to randomNumber, 3:19 we've changed our fact variable from a string to an int. 3:24 To change it back to a string, all we need to do is call toString on our random 3:28 number variable, .toString. 3:32 All right, let's run this and see what happens. 3:39 And if we click on our button, 3:43 we see that our text view gets updated with different numbers. 3:45 Cool, and it looks like the list of numbers starts at zero instead of one. 3:50 So we get zero, one, and two instead of one, two, and three. 3:56
You need to sign up for Treehouse in order to download course files.Sign up