On the php documentation,i found this note:
On both 32 and 64-bit systems (OS X and Linux), mt_getrandmax()
returns 2147483647
I have confirmed this using the simple function provided
function gethighest()
{
return mt_getrandmax();
}
$hello = gethighest();
echo '<b>'.$hello.'</b>';
I am using this snippet to generate a unique id
$number = mt_rand(163245,978534);
$unique_id = crypt($number);
echo md5($unique_id).'<br/>';
My question is,what does it mean to have a max value for mt_rand?.Will the ids begin to repeat once the max value is reached?.
Your code:
$number = mt_rand(163245,978534);
means that the number generated will be between those 2 numbers, it will for as many times as you run it, generate a number between those 2 values.. No returned value will be outside that range. But yes, values can be repeated..
eg try it with mt_rand(1,5) and tell it to do it 20 times and output.
Consider using uniqid? http://php.net/manual/en/function.uniqid.php
If you intent to generate a unique ID, you should use uniqid:
$unique_id = uniqid(microtime(true));
Related
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.
----------
php code
$number = rand(100,100000); //a six digit random number between 100 to 100000
echo $number;
----------
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.
I would do this:
You said you have branches. The receipt id could look something like this:
$dateString = date('Ymd'); //Generate a datestring.
$branchNumber = 101; //Get the branch number somehow.
$receiptNumber = 1; //You will query the last receipt in your database
//and get the last $receiptNumber for that branch and add 1 to it.;
if($receiptNumber < 9999) {
$receiptNumber = $receiptNumber + 1;
}else{
$receiptNumber = 1;
}
Update the receipt database with the receipt number.
$dateString . '-' . $branchNumber . '-' . $receiptNumber;
This will read:
20180406-101-1
This will be unique(Provided you do less than 10,000 transactions a day.) and will show your employees easily readable information.
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.
There are many ways, example:
$freq = [];
$number = rand(100,100000);
$times = 10;
while($times-- > 0)
{
while(in_array($number, $freq))$number = rand(100,100000);
$freq[] = $number;
echo $number . "<br>";
}
This will print 10 random unique numbers.
random_int
(PHP 7)
<?php
$number = random_int(100, 100000);
echo $number;
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 time() function in php.
The time() function is used to format the timestamp into a human desired format. The timestamp is the number of seconds between the current time and 1st January, 1970 00:00:00 GMT. It is also known as the UNIX timestamp.
<?php
$t=time();
echo $t;
?>
Also you add a rand() function and insert it in front of the $t to make it more random as if few users work at same time then the timestamp might collide.
<?php
$number = rand(100,100000);
$t=time();
$random = $number.''.$t;
echo $random;
?>
The above will reduce the chance to timestamp collide hence making the probability of number uniqueness almost 100%.
And if you make your column unique in your database then the php wont insert the number hence this bottleneck will ensure you will always get a unique random number.
bill_id not null unique
If you are using it for something like user id, then you can use uniqid for that. This command gets a prefixed unique identifier based on the current time in microseconds.
Here's how to use it:
string uniqid ([ string $prefix = "" [, bool $more_entropy = FALSE]] )
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.
Usage:
<?php
/* A uniqid, like: 4b3403665fea6 */
printf("uniqid(): %s\r\n", uniqid());
/* We can also prefix the uniqid, this the same as
* doing:
*
* $uniqid = $prefix . uniqid();
* $uniqid = uniqid($prefix);
*/
printf("uniqid('php_'): %s\r\n", uniqid('php_'));
/* We can also activate the more_entropy parameter, which is
* required on some systems, like Cygwin. This makes uniqid()
* produce a value like: 4b340550242239.64159797
*/
printf("uniqid('', true): %s\r\n", uniqid('', true));
?>
this code must work
some description about code:
generate unique id
extract numbers form unique id with regex
gathering numbers from regex with a loop
<?php
$unique = uniqid("",true);
preg_match_all("!\d+!", $unique ,$matches);
print_r($matches);
$numbers = "";
foreach($matches[0] as $key => $num){
$numbers .= $num;
}
echo $numbers;
I need to multiply this POST variable by 12. As an example, if the amount was 10, the result should say:
Amount: 120
Here's my code so far:
Amount :'.$_POST['my_amount'].'<br/>
I tried to run the calculation in another variable, but this doesn't seem to work:
$result = ($_POST['my_amount'])*12;
or maybe it works and my output code is not working:
$vl_text='';
Amount :'.$_POST['my_amount'].'<br/>'.;
If you want your output to resemble your first example.,.. Amount:120 your missing chunks in each of the following 3 examples. first ensure that your $_POST variable is a valid one and set it to a new variable so you can print out the variable if you need to ...
// if you only expect $_POST['my_amount'] to contain integers...
if(is_int(intval($_POST['my_amount']))){
$my_amount = intval($_POST['my_amount']) * 12;
// or if you expect $_POST['my_amount'] to possibly contain a decimal
if(is_float(floatval($_POST['my_amount']))){
$my_amount = floatval($_POST['my_amount']) * 12;
intval ensures that a variable is cast as an integer if it can be, while not entirely necessary as multiplying in php will do this...its good practice to check any variables that you are using for and math functionality.
floatval does the same for for numbers with decimal. as an integer has to be a whole number if your variable could numbers that could contain decimals... use floatval
all of your examples then need to specify to print/echo the string....so
// your second line
echo 'Amount :'.$my_amount .'<br/>';
// your fourth line...
$vl_text='Amount: '.$my_amount;
echo $vl_text;
}
The most logical explanation is that you get string from POST. A good way to achieve what you want is to convert the POST value to int but keep in mind that it could not be numerical.
$int = (is_numeric($_POST['my_amount']) ? (int)$_POST['my_amount'] : 0); //If POST value is numeric then convert to int. If it's not numeric then convert it to 0
$_POST['my_amount'] = 150;
$data = $_POST['my_amount'] * 12;
echo $data;
Result will be 1800
I want to create user accounts with a public_id which is always a unique, integer random (not incremental) value.
I can use loops to check if the random integer is unique, but that doesn't seem like a really nice solution.
I found some alphabetic-numeric generators, and I guess I could convert them to integers using some string to integer converter, but are there an integer -specific ways?
I also worry about possible collisions, but it looks like the chance will be always there in a long run.(?)
You can either use one of native php functions like mt_rand or use more reliably way - generating integer based on microtime function.
To ensure that the value is unique you need to add a unique index on a column in DB and write 'ON DUPLICATE UPDATE' to insert/update queries which will add some digits to the value if it is not unique
There are 2 possible solutions:
1) If your "long run" is really really long - it means this is
possible, that you are out of PHP_INT_MAX and there is no
only-integer-specific way.
2) If you are not out of PHP_INT_MAX - then you need some storage for
checking the ids.
In case of 1 you can use library hashids. To avoid collisions - you'll need some incremental counter on input. Then you can convert strings by each letter back to integer.
In case of 2 - you can use some in-memory database like redis for performance.
Using timeStamp will really do a great job since it uses time to generate it random numbers .you can also concatenate the below function with other random generated numbers.
function passkey($format = 'u', $utimestamp = null){
if (is_null($utimestamp)) {
$utimestamp = microtime(true);
}
$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000000);
return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format),$timestamp);
}
echo passkey(); // 728362
You can use a linear congruential generator with a large period.
Here is one that generates unique integers which always have 6 digits. It will not generate duplicates until it has generated all numbers between 100000 and 996722, which gives you almost 900 000 different numbers.
The condition is that you can provide the function the number it last generated. So if you store the number in the database, you have to somehow retrieve the last assigned one, so you can feed it to this function:
function random_id($prev) {
return 100000 + (($prev-100000)*97 + 356563) % 896723;
}
$prev = 100000; // must be a 6 digit number: the initial seed.
// Generate the first 10 pseudo-random integers.
for ($i = 0; $i < 10; $i++) {
$prev = random_id($prev);
echo $prev . "\n";
}
The above generation of the first 10 numbers yields:
456563
967700
331501
494085
123719
963860
855744
232445
749606
697735
You can do this for other ranges by following the rules in the referenced article on getting a full period in linear congruential generators. Concretely, if you want to generate numbers with n digits, where the first digit cannot be zero (so between 10n-1 and 10n-1), then I find it easiest to find a large prime just below 9⋅10n-1 to serve as the last number of the formula. The other two numbers can then be any positive integer, but better keep the first one small to avoid overflow.
However, PHP integers are limited to PHP_INT_MAX (typically 2147483647), so for numbers with 10 or more digits you will need to use floating point operators. The % operator should not be used then. Use fmod instead.
For example, to generate numbers with 12 digits, you could use this formula:
function random_id($prev) {
return 100000000000 + fmod((($prev-100000000000)*97 + 344980016453), 899999999981);
}
$prev = 100000000000; // must be a 12 digit number: the initial seed.
// Generate the first 10 pseudo-random integers.
for ($i = 0; $i < 10; $i++) {
$prev = random_id($prev);
echo $prev . "\n";
}
How can I generate a 6 digit unique number? I have verification mechanisms in place to check for duplicate entries.
$six_digit_random_number = random_int(100000, 999999);
As all numbers between 100,000 and 999,999 are six digits, of course.
If you want it to start at 000001 and go to 999999:
$num_str = sprintf("%06d", mt_rand(1, 999999));
Mind you, it's stored as a string.
Another one:
str_pad(mt_rand(0, 999999), 6, '0', STR_PAD_LEFT);
Anyway, for uniqueness, you will have to check that your number hasn't been already used.
You tell that you check for duplicates, but be cautious since when most numbers will be used, the number of "attempts" (and therefore the time taken) for getting a new number will increase, possibly resulting in very long delays & wasting CPU resources.
I would advise, if possible, to keep track of available IDs in an array, then randomly choose an ID among the available ones, by doing something like this (if ID list is kept in memory):
$arrayOfAvailableIDs = array_map(function($nb) {
return str_pad($nb, 6, '0', STR_PAD_LEFT);
}, range(0, 999999));
$nbAvailableIDs = count($arrayOfAvailableIDs);
// pick a random ID
$newID = array_splice($arrayOfAvailableIDs, mt_rand(0, $nbAvailableIDs-1), 1);
$nbAvailableIDs--;
You can do something similar even if the ID list is stored in a database.
Here's another one:
substr(number_format(time() * rand(),0,'',''),0,6);
There are some great answers, but many use functions that are flagged as not cryptographically secure. If you want a random 6 digit number that is cryptographically secure you can use something like this:
$key = random_int(0, 999999);
$key = str_pad($key, 6, 0, STR_PAD_LEFT);
return $key;
This will also include numbers like 000182 and others that would otherwise be excluded from the other examples.
You can also use a loop to make each digit random and generate random number with as many digits as you may need:
function generateKey($keyLength) {
// Set a blank variable to store the key in
$key = "";
for ($x = 1; $x <= $keyLength; $x++) {
// Set each digit
$key .= random_int(0, 9);
}
return $key;
}
For reference, random_int — Generates cryptographically secure pseudo-random integers that are suitable for use where unbiased results are critical, such as when shuffling a deck of cards for a poker game." - php.net/random_int
<?php
$file = 'count.txt';
//get the number from the file
$uniq = file_get_contents($file);
//add +1
$id = $uniq + 1 ;
// add that new value to text file again for next use
file_put_contents($file, $id);
// your unique id ready
echo $id;
?>
i hope this will work fine. i use the same technique in my website.
In PHP 7.0+ I would suggest random_int($min, $max) over mt_rand().
$randomSixDigitInt = \random_int(100000, 999999);
From php.net:
Caution
This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using random_int(), random_bytes(), or openssl_random_pseudo_bytes() instead.
So this depends mostly on context. I'll also add that as of PHP 7.1.0 rand() is now an alias to mt_rand().
Cheers
$characters = '123456789';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < 6; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
$pin=$randomString;
This will generate random 6 digit number
<?php
mt_rand(100000,999999);
?>
I would use an algorithm, brute force could be as follows:
First time through loop:
Generate a random number between 100,000 through 999,999 and call that x1
Second time through the loop
Generate a random number between 100,000 and x1 call this xt2, then generate a random number between x1 and 999,999 call this xt3, then randomly choose x2 or x3, call this x2
Nth time through the loop
Generate random number between 100,000 and x1, x1 and x2, and x2 through 999,999 and so forth...
watch out for endpoints, also watch out for x1
<?php echo rand(100000,999999); ?>
you can generate random number
You can use $uniq = round(microtime(true));
it generates 10 digit base on time
which is never be duplicated
Try this using uniqid and hexdec,
echo hexdec(uniqid());
Among the answers given here before this one, the one by "Yes Barry" is the most appropriate one.
random_int(100000, 999999)
Note that here we use random_int, which was introduced in PHP 7 and uses a cryptographic random generator, something that is important if you want random codes to be hard to guess. random_bytes was also introduced in PHP 7 and likewise uses a cryptographic random generator.
Many other solutions for random value generation, including those involving time(), microtime(), uniqid(), rand(), mt_rand(), str_shuffle(), array_rand(), and shuffle(), are much more predictable and are unsuitable if the random string will serve as a password, a bearer credential, a nonce, a session identifier, a "verification code" or "confirmation code", or another secret value.
The code above generates a string of 6 decimal digits. If you want to use a bigger character set (such as all upper-case letters, all lower-case letters, and the 10 digits), this is a more involved process, but you have to use random_int or random_bytes rather than rand(), mt_rand(), str_shuffle(), etc., if the string will serve as a password, a "confirmation code", or another secret value. See an answer to a related question, and see also: generating a random code in php?
I also list other things to keep in mind when generating unique identifiers, especially random ones.
This is the easiest method to generate 6 digits random number
$data = random_int(100000, 999999);
echo $data;
how can i convert a string(i.e. email address) to unique integers, to use them as an ID.
The amount of information a PHP integer may store is limited. The amount of information you can store in a string is not (at least if the string isn't unreasonably long.)
Thus you would need to compress your arbitrary-length string to an non-arbitrary-length integer. This is impossible without data loss.
You may use a hashing algorithm, but hashing algorithms may always have collisions. Especially if you want to hash a string to an integer the collision probability is pretty high - integers can store only very little data.
Thus you shall either stick with the email or use an auto incrementing integer field.
Try the binhex function
from the above site:
<?php
$str = "Hello world!";
echo bin2hex($str) . "<br />";
echo pack("H*",bin2hex($str)) . "<br />";
?>
outputs
48656c6c6f20776f726c6421
Hello world!
Why not just have an auto-increment ID field on the database?
This code generates 64bit number which can be use as it or as a bigInt / similar data-type for databases like MySQL etc.
function get64BitNumber($str)
{
return gmp_strval(gmp_init(substr(md5($str), 0, 16), 16), 10);
}
echo get64BitNumber('Hello World!'); // 17079728445181560374
echo get64BitNumber('Hello World#'); // 2208921763183434891
echo get64BitNumber('http://waqaralamgir.tk/'); // 12007604953204508983
echo get64BitNumber('12345678910'); // 4841164765122470932
If the emails are ascii text, you could use PHP ord function to generate a unique integer, but it will be a very large number!
The approach would be to work through the email address one character at a time, calling ord for each of them. The ord function returns an integer uniquely expressing the character's value. You can pad each of these numbers with zeros and then use string concatenation to plug them into each other.
Consider "abc".
ord("a");
>> 97
ord("b");
>> 98
ord("c");
>> 99
Pad these numbers with a 0, and you have a unique number for it, that is: 970980990.
I hope that helps!
You can use crc32 function.
Example:
$email = "user#gmail.com";
echo $email . " = " . crc32($email);
Live example: https://repl.it/repls/HonorableRespectfulBundledsoftware
Why not create your own associative table locally that will bind the emails with unique integers?
So the work flow would be in the lines of:
1 get the record from the ldap server.
2 check it locally if it has already an int assigned.
2.1 if yes use that int.
2.2 if no, generate an associative row in the table locally.
3 do your things with the unique ids.
Does that make sense?
You can use this function:
function stringToInteger($string) {
$output = '';
for ($i = 0; $i < strlen($string); $i++) {
$output .= (string) ord($string[$i]);
}
return (int) $output;
}
A bit ugly, but works :)