Java for Humans {Generating Random Numbers}

Lincoln W Daniel
ModernNerd Code
Published in
4 min readJan 17, 2016

--

View Table of Contents | Download Supporting Code | Subscribe to ModernNerd Youtube Channel for Coding Videos | By Lincoln W Daniel

As a kid, you may have played many games to fill your day with some fun and excitement. In some cases, the most exciting games may have been the ones that employed some bit of randomness and unpredictability. Board games like monopoly use a dice to create randomness in the outcome of the game. The series of random numbers you roll determine how successful you are in the game. Computer games, on the other hand, don’t have access to a physical dice, so there must be another way for them to create randomness. For that reason, Java and other programming languages have random number generators we can use to create randomness in our programs.

Why Random Numbers are Important & Useful

Random number generators allow us to generate random numbers that we can use to create randomness in our code. Java allows us to easily generate random whole (int or long) and floating point (double) numbers. We can also generator random boolean values. With the random numbers that we generate, we can decide when to execute some lines of code and when to execute the others.

A good example of when you may need a random number program is in deciding whether or not you should go out with friends or stay in and do your homework. You can build a program to generate a random number between zero (inclusive) and ten…

--

--

Lincoln W Daniel
ModernNerd Code

Chief Bull @ BullAcademy.org ® Elevating writers @ ManyStories.com. Author @JavaForHumans Ex: Editor in Chief MarkGrowth (acq.), Engineer @Medium @GoPuff