How to generate a unique number in php?
I am generating random numbers using php random function, but I want the generated number should be unique and it should not be repeated again.
but I am using this function for multiple times in my code for users so at very rare case there should be a chance of generating same number again. how can i avoid that.
asked Apr 10, 2018 at 3:36
nithin singh Gmnithin singh Gm
1291 gold badge2 silver badges10 bronze badges
I would do this:
You said you have branches. The receipt id could look something like this:
Update the receipt database with the receipt number.
This will read:
This will be unique(Provided you do less than 10,000 transactions a day.) and will show your employees easily readable information.
answered Apr 10, 2018 at 4:40
If you are storing users in DB you should create column [ID] as primary key with auto increment and that would be best solution.
In other case I'd recommend you to simply store all user id's in ascending order from N to M by reading last ID and adding 1 to it because I see no real gain from random order that only adds complexity to your code.
answered Apr 10, 2018 at 5:21
2,6241 gold badge12 silver badges31 bronze badges
There are many ways, example:
This will print 10 random unique numbers.
answered Apr 10, 2018 at 3:41
Khaled AlamKhaled Alam
8427 silver badges12 bronze badges
random_int (PHP 7)
answered Apr 10, 2018 at 4:42
6,9594 gold badges17 silver badges23 bronze badges
All you need to do is use timestamp in php as timestamp never cross each other hence it will always generate unique number.You can use
Also you add a
The above will reduce the chance to timestamp collide hence making the probability of number uniqueness almost 100%.
you make your column
answered Apr 10, 2018 at 3:48
Adern NerkAdern Nerk
3123 silver badges13 bronze badges
If you are using it for something like user id, then you can use
Here's how to use it:
Where prefix is used if you are generating ids for a lot if hosts at the same time, you can use this to differentiate between various hosts if id is generated at the same microsecond.
more_entropy increases the likeness of getting unique values.
answered Apr 10, 2018 at 4:23
this code must work
some description about code:
answered Jun 15, 2021 at 4:54
How do you generate a unique number?
Here is how you can use the RAND function to generate a set of unique random numbers in Excel:.
In a column, use =RAND() formula to generate a set of random numbers between 0 and 1..
Once you have generated the random numbers, convert it into values, so that it won't recalculate again and again to make your workbook slow..
How do I generate a random 10 digit 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 do I generate a random 4 digit number in PHP?
rand() or mt_rand() functions can be used to Generate 4 Digit Random Number in PHP.
How do I generate a random 6 digit number in PHP?
rand() or mt_rand() functions can be used to Generate 6 Digit Random Number in PHP.