answersLogoWhite

0

How do you generate random numbers in PHP?

Updated: 12/24/2022
User Avatar

Pandoga

Lvl 1
15y ago

Best Answer

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

15y ago
This answer is:
User Avatar

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.


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!


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 ?>

Related questions

Which function is used to randomize number in Excel?

There is a RAND function which can generate random numbers. The RANDBETWEEN function can generate numbers between a lower and upper limit.There is a RAND function which can generate random numbers. The RANDBETWEEN function can generate numbers between a lower and upper limit.There is a RAND function which can generate random numbers. The RANDBETWEEN function can generate numbers between a lower and upper limit.There is a RAND function which can generate random numbers. The RANDBETWEEN function can generate numbers between a lower and upper limit.There is a RAND function which can generate random numbers. The RANDBETWEEN function can generate numbers between a lower and upper limit.There is a RAND function which can generate random numbers. The RANDBETWEEN function can generate numbers between a lower and upper limit.There is a RAND function which can generate random numbers. The RANDBETWEEN function can generate numbers between a lower and upper limit.There is a RAND function which can generate random numbers. The RANDBETWEEN function can generate numbers between a lower and upper limit.There is a RAND function which can generate random numbers. The RANDBETWEEN function can generate numbers between a lower and upper limit.There is a RAND function which can generate random numbers. The RANDBETWEEN function can generate numbers between a lower and upper limit.There is a RAND function which can generate random numbers. The RANDBETWEEN function can generate numbers between a lower and upper limit.


How can random numbers be generated in Java?

Random numbers can be generated in Java using the "random" class. One needs a single "random" object to generate a series of random numbers as a unit.


What does it mean to generate random numbers in Java?

Generating random numbers in Java is somewhat of a misnomer because the numbers are actually semi-random.It means to use the program to obtain random integers to use in hypothetical situations such as statistics.


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

Cluster Sampling


How do you generate random numbers in Excel?

You can use the RAND function or the RANDBETWEEN function.


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.


Can Microsoft Excel randomize orders of items in a list?

Just use =RAND() to generate random numbers in your list. Then you can sort it with the random numbers. Comments on answer: Let's just say the list is in column C and has 12 entries. Your answer says to "just use =RAND() to generate random numbers in your list". OK, so where "in the list" would I put the =RAND() function command? And in which cells would that function display the "random numbers in my list" that you suggest? And then, how would I go about sorting "it" with the random numbers that somewhere have been generated? Sorry for being the devil's advocate, but I just want to get clear info on how to do the task you answered.


What is a randbetween in a spreadsheet?

Randbetween allows you to generate random numbers with a range. You specify the lowest and highest that you want the number to be between. So if you wanted to generate a random number between 1 and 100, you would enter: =RANDBETWEEN(1,100) If you wanted to generate a random number between 25 and 60, you would enter: =RANDBETWEEN(25,60) The function will generate a new random number every time you make a change to the spreadsheet or press the F9 key. So if you want to retain the random numbers it generates, you could copy them and then do a Paste Special and choose Values. You could paste this on top of the original values or in a new location.


Why are random numbers useful in PHP?

Random numbers can be used for many different things. Personally i use the rand() directive for security measures to define a session identifier. Also i use the rand feature (sometimes) for user authentication while using captcha images. basically, when you need a random number, use rand() ;)


How do pseudorandom numbers affect that accuracy of simulation?

Random numbers are used to determine the statistics and generate frequency rules. Pseudorandom numbers can skew the resultant frequencies and greatly alter the accuracy of simulation.


How are cell phone numbers assigned?

When telephone numbers were invented, they were short - as few as two or three digits - and were used by people to call a few neighbors. As phone systems have grown and interconnected to encompass the world, telephone numbers have grown longer and more complex, and in addition to telephones they now designate many other devices, such as computers and fax machines.


Why do you use pseudorandom numbers in simulation?

Generation of random numbers is not a simple process. In order for a number to be truly random it must be uniformly distributed (each random number in the interval of random numbers has equal chance of being selected), efficiently generated (the random numbers hsould not degenerate into constant values or recycle too frequently) and absent of patterns. Computers can generate random numbers using a mathematical process that artificially creates psuedorandom numbers more efficiently than true random numbers can be generated in a process akin to spinning the roulette wheel.