Jump to content

random number generator


ProblemHelpPlease

Recommended Posts

I have been using the random number generator below which generates a 50 character string of numbers and letters and then the other day it generated the same value as it had done a few months ago. The chance of this happening is very remote so I wondered if the generator was not random after all.

function randomgeny(){$chars = "0123456789abcdefghijklm0123456789nopqrstuvwxyz0123456789";srand((double)microtime()*1000000);$i = 0;$pass = '';while ($i <= 50) {   $num = rand() % 33;   $tmp = substr($chars, $num, 1);   $pass = $pass . $tmp;   $i++;  }return $pass;}

Can anyone confirm is this is indeed random or anyone have any better ways to randomly generate a 50 character string

Link to comment
Share on other sites

Its probably not as random as it could be, since you're taking a substring of a static string of characters. I use this function to generate a random hexadecimal string and in my tests I generated 10000 strings of 7 characters and not once did I have a duplicate.

function randHexDec($len) {	$hex = '';	for ($i=0; $i<$len; $i++) {		$d=rand(1,16)%8;		$hex .= ($d<5) ? chr(rand(48,57)) : chr(rand(65,70));	}	return $hex;}

You can modify it to include all letters (instead of just a-f) by changing the character codes from rand(65,70) to rand(65,90) and changing ($d<5) to ($d<3).EDIT: Oh, and in this line:$d=rand(1,16)%8;change 16 to 36That's the total number of characters possible. 3/8 of those characters are numbers, so that's why I use %8 and check if $d<3. If $d is less than three, generate a random number, if it's greater or equal, generate a random letter.EDIT2:Although, in performing some further tests, even this function is not very random. I just ran 100000 strings at 7 characters but I only get 16384 unique strings. There should be some 16P7 = 57657600 unique strings possible though....I don't understand why I'm only able to generate 16384....It also appears that this function is seeded somehow. The strings are just repeated in exactly the same order, over and over. It starts out with 26BA13A; 26A86BC; 110B955;....and on up to the 16384th string and then starts over again with 26BA13A; 26A86BC; 110B955;....Perplexing... :)

Link to comment
Share on other sites

Can anyone confirm is this is indeed random or anyone have any better ways to randomly generate a 50 character string
You might like to try the mt_rand() function. It appears to be an overall better random number generator.Roddy
Link to comment
Share on other sites

You might like to try the mt_rand() function. It appears to be an overall better random number generator.
It appears that this is indeed a much better generator. When I changed my function above to use mt_rand instead of rand, it eliminated the issues I mentioned in the second edit.Thanks, iwato! :)
Link to comment
Share on other sites

Computers cannot generate random numbers, because computers do not do anything randomly. Any algorithm is deterministic, if you have the set of inputs you can always calculate the outputs, nothing is actually random. All random number generators in computers are more correctly referred to as "pseudo-random".That being said, the mt_rand function uses the Mersenne Twister algorithm, which produces "better" random values than many other methods.

Link to comment
Share on other sites

Computers cannot generate random numbers, because computers do not do anything randomly. Any algorithm is deterministic, if you have the set of inputs you can always calculate the outputs, nothing is actually random. All random number generators in computers are more correctly referred to as "pseudo-random".
Right. But I should have been able to squeeze more than 16k strings from a possible 57 million.
Link to comment
Share on other sites

The joys of coding!!!!You given me quite a lot to think about, theirs something funny about soemthing as complex as a computer not being able to be random.I think the only solution in my case is to check that the random string I am generating does not already exists in the database of strings I have already, and rerun if it does and check again. Its not going to be easy to implement though.

Link to comment
Share on other sites

I think the only solution in my case is to check that the random string I am generating does not already exists in the database of strings I have already, and rerun if it does and check again. Its not going to be easy to implement though.
Sure it is.Create an array of the existing id's using a query like this "SELECT stringID FROM table"That should pull all existing id's from your table.Then when you generate your string, you can use in_array to check if the generated string is in the array and if it is, generate another one. Keep checking until you get an unused id.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...