I am creating a file-oriented database of some test results performed by various users. For this I need to generate unique id for every entry in the database. The ids must satisfy following requirements:
- Ids should be fairly small (6 characters at most)
- For every test case and user combination each time same id should be generated
What I tried was a simple BKDR hash function with seed value 31 and used ord() function as follows:
@chars = split(//,$hash_var);
$hash = 0;
$seed = 31;
foreach $char ( @chars ) {
if( $char !~ m/\d/ ) {
$hash = ( $seed * $hash ) + ord( $char );
}
else {
$hash = ( $seed * $hash ) + $char ;
}
}
$hash = ( $hash & 0x7FFFFFFF ) % 1000;
$hash = "$chars[0]$chars[$#chars]$hash" ;
This sometimes leads to same results for various combinations i.e uniqueness is not observed. Is their any other way to accomplish this? Does changing seed value help accomplish uniqueness.