Reputation: 783
I am using the below code to generate random string which I then insert to the database, and I would like to know if it is collision free? Is there a better way that is collision free?
function generate_random_name()
{
$length = 12;
$charactersa = "mRdxIU2r3dxa1B2c3D4e5F6g7H8i9Jqk1L2m13N1oF5P6qW7R1Ls9T20u21V2w3X4y2Z2x6";
$real_string_legntha = strlen($charactersa) - 1;
$stringa=rand(12345,09876);
for ($oluvica = 0; $oluvica < $length; $oluvica++)
{
$stringa .= $charactersa[mt_rand(0, $real_string_legntha)];
}
return $stringa;
}
$unique = generate_random_name();
Upvotes: 0
Views: 331
Reputation: 68526
Make use of uniqid()
[With more entropy
flag enabled] coupled with time()
<?php
echo uniqid(time(), true);
This will be more than enough. Coupling hashes like sha-256 and sha-512 will generate more unique outputs.
Some more info
Upvotes: 1