answersLogoWhite

0

Use the function "rand()" or "mt_rand()". Both functions will generate a random number (as a return value, so be sure to make it point to a variable - see examples). However, "rand()" is slower and uses a default PHP randomizing system. mt_rand() is four times as fast, and uses the Mersenne Twister randomizing format - which is much more random. Both functions have two optional parameters - a minimum integer and a maximum integer, respectively. By filling in these parameters, you will get a random number between the values you give (so, giving one and five will give you a random number that is no less than one, and no greater than five). These, again, are optional - and if left blank, the tiniest integer possible is no less than 0, and the largest integer possible is generated by the PHP function "getrandmax()" / "mt_getrandmax()" automatically. Examples, of which all are acceptable: ---- /* Examples using no parameters */ $randomNumber = rand(); $randomNumber = mt_rand();

/* Examples using parameters */ $randomNumber = rand(1, 5); $randomNumber = mt_rand(6, 10); ---- If your PHP build is version 4.2 or under, you will need to seed the random number generator with a value to base off of. "srand()" and "mt_srand()" (to be used with rand() and mt_rand() functions, respectively) can be used in this case. They both only have one optional parameter, which is any seed value - any number. If omitted, a random seed will be generated. More acceptable examples: ---- // Example using no parameters

srand();

$randomNumber = rand(); // Example using the seed parameter

srand(12345);

$randomNumber = rand(); // Example using the "mt" random format functions (without parameters)

mt_Rand();

$randomNumber = mt_rand(); // Example using the "mt" random format (with parameters)

mt_rand(12345);

$randomNumber = mt_rand(); ----

User Avatar

Wiki User

16y ago

Still curious? Ask our experts.

Chat with our AI personalities

JordanJordan
Looking for a career mentor? I've seen my fair share of shake-ups.
Chat with Jordan
FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran
TaigaTaiga
Every great hero faces trials, and you—yes, YOU—are no exception!
Chat with Taiga

Add your answer:

Earn +20 pts
Q: How do you generate random numbers in PHP?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Math & Arithmetic

A pollster uses a computer to generate 500 random numbers then interviews the voters corresponding to those numbers?

Cluster Sampling


How would generate a random sample?

Each item in the population has an equal chance to be chosen. Usually a computer algorithm is used to generate a set of random numbers (most spreadsheet . Then the items are chosen using the set of random numbers, either as they come off the assembly line or if they are serialized, you can just go pick those items.


What is the difference between stochastic and random?

Wikipedia states that stochastic means random. But there are differences depending on the context. Stochastic is used as an adjective, as in stochastic process, stochastic model, or stochastic simulation, with the meaning that phenomena as analyzed has an element of uncertainty or chance (random element). If a system is not stochastic, it is deterministic. I may consider a phenomena is a random process and analyze it using a stochastic simulation model. When we generate numbers using a probability distribution, these are called random numbers, or pseudo random numbers. They can also be called random deviates. See related links.


How do you pick a random value from a PHP array?

To pick random results from an array in PHP you will need to use the array_rand() function, you can select the amount of results you wish it too select too. <?php $array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $rand_results = array_rand($array, 3); // will select 3 random results from the array and store in a new array ?>


Does the CIA buy prime numbers?

I see no reason why they would do that. Prime numbers can be used for encryption, but there are algorithms that can quickly generate a more or less random prime number. No need to pay for the "discovery" of a specific number!