In this video, Doug Winnie walks through how to use the Random class to create a random number generator. With the Random class, you can use the nextInt method to get a random integer that you can define within a specific range.
- [Teacher] When you roll a die,…you're taking a chance.…The chance is that you're going to get…a number from one to six appear on the die…when it is finished rolling.…In code, you simulate the die…using a random number generator.…In Java, to create the random number generator,…you'll use the random class.…This class, when it is instantiated,…allows you to generate a random number…using a range that you define.…In our code, we need to create…the generator for our app.…Open the MainActivity.java file.…We'll create a field in our class…to hold the instance of the random class.…
At the top, above the onCreate method,…create a field to hold our random class instance.…We'll use the class name Random,…and then we'll call it rand.…You'll need to import this class…using the Alt Enter shortcut for the IDE.…When the activity starts,…we need to create our instance of the random class…and assign it to the field.…We can do this at the bottom of the onCreate method.…We'll access our instance, rand,…and we'll set that to a new instance…
Author
Released
12/15/2016- Installing Android Studio
- Coding in Java and XML
- Getting a head start with starter templates
- Working with emulators
- Creating a project, variable, and toast
- Connecting XML widgets to code
- Adding images
- Adding game logic with conditional statements and operators
- Changing the data or design
Skill Level Beginner
Duration
Views
Related Courses
-
Introduction
-
Welcome55s
-
Exercise files1m 34s
-
-
1. Android Basics
-
Android app basics1m 41s
-
App Development overview2m 57s
-
-
2. Setup
-
Test your setup2m 56s
-
3. The Major Parts
-
Android Studio3m 5s
-
Java44s
-
XML1m 34s
-
Android SDK1m 38s
-
Starter templates1m 4s
-
Emulators1m 21s
-
Hardware41s
-
Documentation1m 25s
-
-
4. Make an App
-
About the game1m 33s
-
Create a single die1m 45s
-
Add dice images6m 23s
-
Accessing ImageViews in Java2m 30s
-
Make dice images dynamic6m 14s
-
Add score display2m 48s
-
Add score logic3m 11s
-
Use Material Design button4m 27s
-
Add design1m 53s
-
Add icon2m 7s
-
-
5. Tinkering with Your App
-
Conclusion
-
Next steps1m 4s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Create a random number generator