dummies logo
Articles

Article Categories

close
TechnologyAcademics & The ArtsHome, Auto, & HobbiesBody, Mind, & SpiritBusiness, Careers, & Money
Collections

Collections

Explore all collections
close
BYOB (Be Your Own Boss)Be a Rad DadCareer ShiftingContemplating the CosmosFor Those Seeking Peace of MindFor the Aspiring AficionadoFor the Budding Cannabis EnthusiastFor the College BoundFor the Exam-Season CrammerFor the Game Day Prepper
Custom Solutions
Dummies AI
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
forward arrow
  • Custom Solutions
  • Dummies AI
    • Log In
    Explore Book
    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.

    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.)

    About This Article

    This article is from the book: 

    Beginning Programming with Java For Dummies

    About the book author:

    Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.

    This article can be found in the category: 

    Java
    Trending
    Captain America For Dummies Cheat Sheet
    Creating Custom Excel Templates
    From Category
    Java
    Important Features of the Java Language
    How to Download and Install TextPad
    How to Install JavaFX and Scene Builder
    Getting a Value from a Method in Java
    A Few Things about Java GUIs

    Quick Links

    About For Dummies
     Terms of Use
    Copyright © 2000-2025 by John Wiley & Sons, Inc., or related companies. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies.
    © 2025 MARVEL

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant