-3
I have this function to generate UUID's:
function uuid($v=4,$d=null,$s=false)//$v-> version|$data-> data for version 3 and 5|$s-> add salt and pepper
{
switch($v.($x=''))
{
case 3:
$x=md5($d.($s?md5(microtime(true).uniqid($d,true)):''));break;
case 4:default:
$v=4;for($i=0;$i<=30;++$i)$x.=substr('1234567890abcdef',mt_rand(0,15),1);break;
case 5:
$x=sha1($d.($s?sha1(microtime(true).uniqid($d,true)):''));break;
}
return preg_replace('@^(.{8})(.{4})(.{3})(.{3})(.{12}).*@','$1-$2-'.$v.'$3-'.substr('89ab',rand(0,3),1).'$4-$5',$x);
}
This is far from being short!
The idea is to reduce this at maximum!
Criteria to meet:
It MUST have the format
xxxxxxxx-xxxx-vxxx-yxxx-xxxxxxxxxxxx
, being x a hexadecimal number, y MUST be 89AB and v has to be the version! (required)Only
v
can be generated randomly for all versions (non-standard, optional)Version 3 and 5 have to generate ALWAYS the same UUID (except for the rule above, required)
You must provide a method of making the UUID somewhat random (required, except for version 4)
Version 3 uses
md5
to "pack" the data, while version 5 usessha1
(leaving a few chars behind, required)Function name MUST be
uuid
(required)
Scoring:
- Lower number of chars wins
- The score is calculated using (number chars)*0.75
- Readable code is calculated using (number chars)*0.50
- If one of the required criteria from above isn't met, the multiplier is increased by 0.5 for each criteria, except for the last which is 1.25 (maximum will be (number chars)*4.00, which means that 1 char is counting as 4)
- Comments don't count but anything else between
function uuid(...){
and}
counts!
For example:
My function would have a crappy result:
It has 451 chars on linux.
Since it is somewhat hard to read, it is *0.75.
Since I fulfilled all the criteria, it stays *0.75.
Result: 451*0.75 = 338,25!
Your code doesn't seem to meet your own specification; for
$v
of 3 and 5, they
half-byte is chosen at random. Also, the criteria are somewhat hard to understand as written. For example, do you mean that versions 3 and 5 must always return the same UUID, given the same data, or do you mean that they actually always return the same value (fairly useless)? – primo – 2014-02-08T08:49:09.033If I run
uuid(3,'this')
4 times, depending on the implementation you choose, you must have the same UUID, except the only char that can be random. – Ismael Miguel – 2014-02-08T08:51:38.1071So this is a specification that you've invented? According the OSF UUID specification, a version 3 UUID must always return the same value for any given data. – primo – 2014-02-08T08:57:26.453
I know, but I'm letting that one be a little "loose". And it is well identified as being optional and NOT standard. All i want is a UUID generator for all those 3 versions. That one was my example. It works, but it's quite a chunk of frankencode. The idea is to keep it standard. All non-standard "features" are optional, except the "random" part, that can be a simple salt given by the function. – Ismael Miguel – 2014-02-08T09:00:50.623