Jump to content

Random Alphanumeric Generator


bcart

Recommended Posts

Hi there

 

I am currently writing an exam booking system which seems to be working except for one vital part. This is what happens...

 

A user registers the learners details. At this point I use a Random Alphanumeric Generator to return a unique booking reference which is used at the checkout to bring back all the details of their booking.

 

However, sometimes it doesn't seem to work. When I look at the database I see that (in one example) it has generated the same alphanumeric value which is why at the checkout it returns more than what the person thought they had booked.

 

Here's the code for the generator...

 

function randomPrefix($length)
	{
	$random= "";

	srand((double)microtime()*1000000);

	$data = "AbcDE123IJKLMN67QRSTUVWXYZ";
	$data .= "aBCdefghijklmn123opq45rs67tuv89wxyz";
	$data .= "0FGH45OP89";

	for($i = 0; $i < $length; $i++)
	{
	$random .= substr($data, (rand()%(strlen($data))), 1);
	}

	return $random;
}

After this has run I assign it to a session variable.

 

Can anyone help?

 

Cheers!

Link to comment
https://forums.phpfreaks.com/topic/198619-random-alphanumeric-generator/
Share on other sites

try with this

<?php

for ($i=1;$i<=1000;$i++)
{
$arr = range('a','z');
$arr1=range('','');
shuffle($arr);
echo implode("", $arr);echo "<br>";
}
?>

 

odds of producing the same value are astronomical with this :P

 

u can also do one more thing try modifying the database to the random value as the primary key..

 

 

Why not use uniqid or something even more random than yours? Guarenteed on a 100,000 record basis there will be a duplicate with your code. Your random generation has a salt of redundancy for collision prevention.

 

function randomPrefix($length) {
   $random = substr(md5(mt_rand(), false), 0, $length );
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.