By Barry A. Burd

When you call myRandom.nextInt(10) + 1 in Java, you get a number from 1 to 10. As a test, here’s a program that calls myRandom.nextInt(10) + 1

Random myRandom=new Random();

System.out.print(myRandom.nextInt(10) + 1);

System.out.print(" ");

System.out.print(myRandom.nextInt(10) + 1);

System.out.print(" ");

System.out.print(myRandom.nextInt(10) + 1);

// … And so on.

This program was run several times with the results shown below. Stare briefly at the image and notice two trends:

  • There’s no obvious way to predict what number comes next.
  • No number occurs much more often than any of the others.

Java randomness

The Java Virtual Machine jumps through hoops to maintain these trends. That’s because cranking out numbers in a random fashion is a tricky business. Here are some interesting facts about the process:

  • Scientists and nonscientists use the term random number. But in reality, there’s no such thing as a single random number. After all, how random is a number like 9?

A number is random only when it’s one in a disorderly collection of numbers. More precisely, a number is random if the process used to generate the number follows the two preceding trends. When they’re being careful, scientists avoid the term random number and use the term randomly generated number instead.

  • It’s hard to generate numbers randomly. Computer programs do the best they can, but ultimately, today’s computer programs follow a pattern, and that pattern isn’t truly random.

To generate numbers in a truly random fashion, you need a big tub of ping-pong balls, like the kind they use in state lottery drawings. The problem is, most computers don’t come with big tubs of ping-pong balls among their peripherals. So, strictly speaking, the numbers generated by Java’s Random class aren’t random. Instead, scientists call these numbers pseudorandom.

  • It surprises us all, but knowing one randomly generated value is of no help in predicting the next randomly generated value.

For example, if you toss a coin twice, and get heads both times, are you more likely to get tails on the third flip? No. It’s still 50-50.

If you have three sons, and you’re expecting a fourth child, is the fourth child more likely to be a girl? No. A child’s gender has nothing to do with the genders of the older children. (I’m ignoring any biological effects, which I know absolutely nothing about. Wait! I do know some biological trivia: A newborn child is more likely to be a boy than a girl. For every 21 newborn boys, there are only 20 newborn girls. Boys are weaker, so we die off faster. That’s why nature makes more of us at birth.)