How do i generate a random 4 digit number in php?
I need to create a random number with x amount of digits. Show So lets say x is 5, I need a number to be eg. 35562 If x is 3, then it would throw back something like; 463 Could someone show me how this is done? asked Nov 21, 2011 at 17:35
user1022585user1022585 12.5k19 gold badges53 silver badges74 bronze badges 0 You can use
This will output a number between 100 and 999. This because 10^2 = 100 and 10^3 = 1000 and then you need to subtract it with one to get it in the desired range. If 005 also is a valid example you'd use the following code to pad it with leading zeros:
answered Nov 21, 2011 at 17:38
3 Here is a simple solution without any loops or any hassle which will allow you to create random string with characters, numbers or even with special symbols.
where Eg. Results after a couple of executions
You can use the same code to generate random string also, like this
Results with
answered Jun 13, 2017 at 13:56
Akshay KhaleAkshay Khale 7,8718 gold badges49 silver badges58 bronze badges 1 You can use In order to limit the values to values with x digits you can use the following:
answered Nov 21, 2011 at 17:37
halfdanhalfdan 32.8k8 gold badges76 silver badges86 bronze badges 3 Treat your number as a list of digits and just append a random digit each time:
Or a purely numerical solution:
answered Nov 21, 2011 at 17:37
BlenderBlender 280k52 gold badges427 silver badges487 bronze badges the simplest way i can think of is using
In above example , it will generate random number in range 0 to 999. And having 5 digits. answered May 30, 2015 at 14:19 1
Tested here. answered Nov 21, 2011 at 17:40
animuson♦animuson 52.8k28 gold badges139 silver badges145 bronze badges 4
benchmark:
both functions was running in 100000 iterations to make results more explicit answered Feb 21, 2017 at 16:36
AlexAlex 3314 silver badges9 bronze badges 1 Well you can use as simple php function mt_rand(2000,9000) which can generate a 4 digit random number
answered Jun 19, 2019 at 11:22
Anoop P SAnoop P S 7041 gold badge11 silver badges30 bronze badges do it with a loop:
answered Nov 21, 2011 at 17:39
Rene PotRene Pot 24.2k7 gold badges67 silver badges90 bronze badges 0
usage:
answered Nov 21, 2011 at 17:40
easybeasyb 681 silver badge8 bronze badges 1
answered Mar 30, 2016 at 8:26
1 You can generate any x-digit random number with
Syntax : Example : read more
statosdotcom 3,0792 gold badges16 silver badges38 bronze badges answered Sep 4, 2019 at 9:43
Following is simple method to generate specific length verification code. Length can be specified, by default, it generates 4 digit code.
answered May 26, 2020 at 15:46
this simple script will do
answered Dec 18, 2014 at 10:08
Susantha7Susantha7 8891 gold badge18 silver badges35 bronze badges This is another simple solution to generate random number of N digits:
Check it here: http://codepad.org/pyVvNiof answered Mar 19, 2015 at 9:37
MRodriguesMRodrigues 1,78716 silver badges21 bronze badges
answered Feb 18, 2017 at 15:31
This function works perfectly with no repeats and desired number of digits.
You can call the function and pass the number of digits for example:
sample results:
Original script got from this gist answered Jul 29, 2018 at 9:15
Reagan OchoraReagan Ochora 1,4571 gold badge18 silver badges19 bronze badges Please not that http://php.net/manual/en/function.rand.php
Instead it is better to use So to extend @Marcus's answer, you should use:
Note that using
answered Aug 23, 2018 at 10:10
Yahya UddinYahya Uddin 23.9k32 gold badges126 silver badges220 bronze badges 5 The following code generates a 4 digits random number:
answered Jan 12, 2020 at 18:40
1 you people really likes to complicate things :) the real problem is that the OP wants to, probably, add that to the end of some really big number. if not, there is no need I can think of for that to be required. as left zeros in any number is just, well, left zeroes. so, just append the larger portion of that number as a math sum, not string. e.g.
simply becomes
done. answered Apr 2, 2014 at 23:03
gcbgcb 13.3k7 gold badges67 silver badges90 bronze badges 1 How do you generate a 4 digit random number?RAND()*(9999-1000)+1000: This will mainly multiply 9999 with the RAND function to generate 4-digit numbers.. INT(RAND()*(9999-1000)+1000: This will take the closest integer of the random number and generate only the 4-digit on random numbers.. How do I generate a random number in PHP?The rand() function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the rand() function has been an alias of the mt_rand() function.
How can I generate 5 random numbers in PHP?rand() or mt_rand() functions can be used to Generate 5 Digit Random Number in PHP.
How do I generate a random 6 digit number in PHP?The rand () function is used to generate a random number in PHP. It can also be used to generate a random number within a specific range (for example a number between 10 and 30.)
|