Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

  • Print
  • + Share This
  • 💬 Discuss
This chapter is from the book

Creating Random Numbers

The last function you’ll learn about here is rand(), a random-number generator:

$n = rand(); // 31
$n = rand(); // 87

The rand() function can also take minimum and maximum parameters, if you prefer to limit the generated number to a specific range:

$n = rand (0, 10);

These values are inclusive, so in this case 0 and 10 are feasible returned values.

As an example of generating random numbers, let’s create a simple “Lucky Numbers” script.

To generate random numbers:

  1. Begin a new document in your text editor or IDE (Script 4.6):

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
       <title>Lucky Numbers</title>
    </head>
    <body>
  2. Include the PHP tags and address error management, if you need to:

    <?php // Script 4.6 - random.php

    Script 4.6. The rand() function generates random numbers.

    1   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
        1.0 Transitional//EN"
    2      "http://www.w3.org/TR/xhtml1/DTD/
           xhtml1-transitional.dtd">
    3   <html xmlns="http://www.w3.org/1999/
        xhtml" xml:lang="en" lang="en">
    4   <head>
    5      <meta http-equiv="Content-Type"
           content="text/html; charset=utf-8"/>
    6      <title>Lucky Numbers</title>
    7   </head>
    8   <body>
    9   <?php // Script 4.6 - random.php
    10  /* This script generates 3 random
        numbers. */
    11
    12  // Address error handling, if you want.
    13
    14  // Create three random numbers:
    15  $n1 = rand (1, 99);
    16  $n2 = rand (1, 99);
    17  $n3 = rand (1, 99);
    18
    19  // Print out the numbers:
    20  print "<p>Your lucky numbers are:<br />
    21  $n1<br />
    22  $n2<br />
    23  $n3</p>";
    24
    25  ?>
    26  </body>
    27  </html>
  3. Create three random numbers:

    $n1 = rand (1, 99);
    $n2 = rand (1, 99);
    $n3 = rand (1, 99);

    This script prints out a person’s lucky numbers, like those found on the back of a fortune cookie’s fortune. These numbers are generated by calling the rand() function three separate times and assigning each result to a variable.

  4. Print out the numbers:

    print "<p>Your lucky numbers are:<br />
    $n1<br />
    $n2<br />
    $n3</p>";

    The print() statement is fairly simple. The numbers are printed, each on its own line, by preceding them with an HTML break tag.

  5. Close the PHP code and the HTML page:

    ?>
    </body>
    </html>
  6. Save the file as random.php, place it in the proper directory for your PHP-enabled server, and test it in your Web browser (Figures 4.11 and 4.12).

    Figure 4.11

    Figure 4.11 The three random numbers created by invoking the rand() function.

    Figure 4.12

    Figure 4.12 Running the script again produces different results.

  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus