O'Reilly logo

D Cookbook by Adam D. Ruppe

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generating random numbers

Random numbers are commonly necessary in computer programs. Phobos' std.random module offers a variety of random number generators and other functions related to randomization. Here, we'll create a little number guessing game with a replay function. The user guesses the generated number, and then the program tells them if they were high or low.

How to do it…

Let's generate random numbers by executing the following steps:

  1. Create a random number generator, seeding it with the replay value, if given, or unpredictableSeed for a new game.
  2. Generate a random number between 0 and 100.
  3. Ask the user for their guess. Tell them if they were low or high and let them continue trying until they get it. Save each line of user input to the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required