InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Generating Random Numbers Within a Range in PHP
PHP

Generating Random Numbers Within a Range in PHP

InfinityCoder November 15, 2016

You want to generate a random number within a range of numbers.

Use mt_rand():

1
2
3
4
$lower = 65;
$upper = 97;
// random number between $upper and $lower, inclusive
$random_number = mt_rand($lower, $upper);

Generating random numbers is useful when you want to display a random image on a page, randomize the starting position of a game, select a random record from a database, or generate a unique session identifier.

To generate a random number between two endpoints, pass mt_rand() two arguments: the minimum number that can be returned and the maximum number that can be returned.

Calling mt_rand() without any arguments returns a number between 0 and the maximum random number, which is returned by mt_getrandmax().

Generating truly random numbers is hard for computers to do. Computers excel at following instructions methodically; they’re not so good at spontaneity.

If you want to instruct a computer to return random numbers, you need to give it a specific set of repeatable commands; the fact that they’re repeatable undermines the desired randomness.

PHP has two different random number generators, a classic function called rand() and a better function called mt_rand().

MT stands for Mersenne Twister, which is named for the French monk and mathematician Marin Mersenne and the type of prime numbers he’s associated with.

The algorithm is based on these prime numbers. Because mt_rand() is less predictable and faster than rand(), we prefer it to rand().

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Preventing Session Fixation in PHP
You want to make sure that your application is not …

Preventing Session Fixation in PHP

Dumping Variable Contents as Strings in PHP
You want to inspect the values stored in a variable. …

Dumping Variable Contents as Strings in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

    Categories

    • Bash
    • PHP
    • Python
    • Uncategorized

    InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

    About Us

    Start learning your desired programming language with InfinityQuest.com.

    On our website you can access any tutorial that you want with video and code examples.

    We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

    Popular Tags

    binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

    Archives

    • June 2017
    • April 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    Copyright © 2019 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
    Programming Tutorials | Sitemap