studyanywhere.ca - Online Web Development Courses
Published on studyanywhere.ca - Online Web Development Courses (https://www.studyanywhere.ca)

Home > Chapter 4 Exercises

Chapter 4 Exercises [1]

Exercise 6: Creating Random Numbers

The last function you will learn about in this chapter is rand(), a random-number generator:

The random function to generate random numbers in PHP.

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

The rand() function can also take minimum and maximum parameters in PHP.

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


In this exercise you will create a simple Lucky Numbers script utilizing random number generation.

Note: A number of Helpful Tips [2] are located at the end of this web page for reference as you complete this exercise. There is also an Advanced Topic [3] for additional learning.

Generate random numbers:

Script 4-6: The rand() function generates random numbers.
The use of random function for generating random numbers as displayed in the editor view.

  1. Create a new document in your editor with the title Lucky Numbers:
    The HTML document with title Lucky Numbers as displayed in the editor view.
  2. Include the PHP tags, and address error management, if necessary:
    The start of PHP tags and managing errors to be visible as displayed in the editor view.
  3. Create three random numbers:
    The numbers to be randomly generated by calling the rand() function as displayed in the editor view.
    This script prints out a person’s lucky numbers, much like those listed on the back of the fortune found within a fortune cookie. These numbers are generated by calling the rand() function three separate times and assigning each result to a variable.
  4. Print out the numbers:
    The numbers are printed with tabs between them as displayed in editor view.
    The print() statement is fairly simple. The numbers are printed with tabs between them. By placing the tab-separated numbers in the HTML <pre> tags, you put some space between the printed numbers in the web browser. The <pre> tags tell the browser to abide by the existing formatting, which in this case, means the browser should honour the tabs. (Web browsers normally ignore tabs.)
  5. Close the PHP code and the HTML page:
    The closing tag of PHP and HTML as displayed in the editor view.
  6. Save your script as random.php to your XAMPP folder (c:\XAMPP\htdocs\202\chapter4).
  7. Using Cyberduck, upload the file to the production server, and test it in your browser, making sure you are looking at the production server version of the file. (Figures 4-11 and 4-12). Simply refresh your browser to produce different results every time.
    Figure 4-11: The rand() function generates random numbers.
    The result that shows the example of rand() function as displayed in the browser view.
    Figure 4-12: Running the script again produces different results.
    The result that produces different values as displayed in the browser view.

 

Quick tips icon.

  • The getrandmax() function returns the largest possible random number that can be created using rand(). This value differs by operating system.
  • PHP has a second function that generates random numbers: mt_rand(). It works similarly to (but, arguably, better than) rand() and is the smarter choice for sensitive situations like cryptography.

 

Additional/Advanced Reading.

The all other mathematical functions used in PHP.


← Return to Chapter 4, Assignments Page [4]

  • Home
  • About Us
  • Programs
  • Opportunities
  • Eligibility
  • Privacy
  • Accessibility
  • Workshops
  • Blog
  • Land Acknowledgement

Copyright © 2005-2025 Make A Change Canada/Faire un Changement Canada, All Rights Reserved. Designed with accessibility in mind.

(Make A Change Canada is a certified educational institution under the Income Tax Act [PTIB Exemption; ESDC Certification File No. 7009/13579].)

contact us.

-->


Source URL:https://www.studyanywhere.ca/advanced-courses/intro-php/chapter-4/using-numbers/exercise-6

Links
[1] https://www.studyanywhere.ca/advanced-courses/intro-php/chapter-4/using-numbers/exercise-6 [2] https://www.studyanywhere.ca/advanced-courses/intro-php/chapter-4/using-numbers/exercise-6#open_here [3] https://www.studyanywhere.ca/advanced-courses/intro-php/chapter-4/using-numbers/exercise-6#open_here2 [4] https://www.studyanywhere.ca/advanced-courses/intro-php/chapter-4/using-numbers