1

特定の文字列に対して一意の8桁または9桁の数字を生成するアルゴリズムを知っている人はいますか?そうでない場合でも、少なくともアルゴリズムにはphpの例がある方がよいでしょう。

4

4 に答える 4

3

crc32()を使用して、len を返すことができます。

<?php 
function crc_string($str, $len){
    return substr(sprintf("%u", crc32($str)),0,$len);
}
echo crc_string('some_string', 8);//65585849
?>

編集

私の答えに対して衝突/信頼性テストを行った後、長さ8で衝突が発生する可能性が高く、おそらく9ではわずかに少なく、10ではさらに少なくなります。私のテストでは、0 から 100k までの増分値をテストし、26 回の衝突があり、最初の衝突は 36k でした。

<?php 
set_time_limit(0);
header('Content-type: text/html; charset=utf-8');
$time_start = microtime(true);

function crc_string($str, $len){
    return substr(sprintf("%u", crc32($str)),0,$len);
}

echo 'Started, please wait...<br />';
$record = array();
$collisions = 0;
for($i=0; $i<100000;$i++){

    $new = crc_string($i, 8);
    if(in_array($new,$record)){
        $match = array_search($new,$record);
        $took_time = microtime(true) - $time_start;
        echo($new.' has collided for iteration '.$i.' matching against a previous iteration ('.$match.') '.$record[$match]).' (Process time: '.round($took_time,2).'seconds)<br />';
        $collisions++;
    }else{
        $record[]=$new;
    }

    ob_flush();
    flush();
}
echo 'Successfully iterated 100k incrementing values and '.$collisions.' collisions occurred; total processing time: '.round((microtime(true) - $time_start),2).'seconds.';
?>

テスト結果:

Started, please wait...
38862356 has collided for iteration 36084 matching against a previous iteration (8961) 38862356 (Process time: 165.47seconds)
18911887 has collided for iteration 36887 matching against a previous iteration (8162) 18911887 (Process time: 172.79seconds)
37462269 has collided for iteration 38245 matching against a previous iteration (33214) 37462269 (Process time: 185.81seconds)
20153794 has collided for iteration 38966 matching against a previous iteration (6083) 20153794 (Process time: 192.87seconds)
41429622 has collided for iteration 40329 matching against a previous iteration (24999) 41429622 (Process time: 206.41seconds)
20784356 has collided for iteration 48908 matching against a previous iteration (27095) 20784356 (Process time: 302.75seconds)
39932561 has collided for iteration 51926 matching against a previous iteration (12367) 39932561 (Process time: 340.88seconds)
14372225 has collided for iteration 53032 matching against a previous iteration (13211) 14372225 (Process time: 355.46seconds)
16636457 has collided for iteration 55490 matching against a previous iteration (39250) 16636457 (Process time: 389.44seconds)
23059743 has collided for iteration 63126 matching against a previous iteration (39808) 23059743 (Process time: 504.1seconds)
13627299 has collided for iteration 63877 matching against a previous iteration (21973) 13627299 (Process time: 516.08seconds)
24647738 has collided for iteration 63973 matching against a previous iteration (47328) 24647738 (Process time: 517.62seconds)
14471815 has collided for iteration 71118 matching against a previous iteration (37805) 14471815 (Process time: 641.93seconds)
13253269 has collided for iteration 73602 matching against a previous iteration (33064) 13253269 (Process time: 687.53seconds)
10732050 has collided for iteration 73706 matching against a previous iteration (9197) 10732050 (Process time: 689.44seconds)
18919349 has collided for iteration 80358 matching against a previous iteration (73190) 18919349 (Process time: 819.89seconds)
40795042 has collided for iteration 81875 matching against a previous iteration (31127) 40795042 (Process time: 851.3seconds)
14609922 has collided for iteration 82498 matching against a previous iteration (17366) 14609922 (Process time: 864.29seconds)
20425272 has collided for iteration 83914 matching against a previous iteration (9858) 20425272 (Process time: 894.32seconds)
24790147 has collided for iteration 84519 matching against a previous iteration (9754) 24790147 (Process time: 907.34seconds)
35605337 has collided for iteration 91434 matching against a previous iteration (36127) 35605337 (Process time: 1060.5seconds)
30935494 has collided for iteration 91857 matching against a previous iteration (91704) 30935494 (Process time: 1070.17seconds)
28520037 has collided for iteration 92929 matching against a previous iteration (28847) 28520037 (Process time: 1095.53seconds)
31109474 has collided for iteration 95584 matching against a previous iteration (30349) 31109474 (Process time: 1159.36seconds)
40842617 has collided for iteration 97330 matching against a previous iteration (13609) 40842617 (Process time: 1203.19seconds)
20309913 has collided for iteration 99224 matching against a previous iteration (94210) 20309913 (Process time: 1250.54seconds)
Successfully iterated 100k incrementing values and 26 collisions occurred; total processing time: 1269.98seconds.

結論は、自動インクリメント値で 1 対 1 のインクリメントを行わない限り、users テーブルを埋めるにつれて、常に同じバイト長以上の衝突が発生するということです。

echo sprintf("%08d",'1');//00000001
echo sprintf("%08d",'2');//00000002
...                      //99999999

衝突した値に別のバイトを追加するか、オブジェクトを無効にする md5()/sha() ハッシュ関数のように az 範囲を含めることで、これを回避できます;p

幸運を

于 2012-08-21T11:55:53.383 に答える
1

はい、衝突は発生しますが、なぜこれが必要なのかを述べていないので、衝突は問題ではないと仮定してください。

文字列の md5 ハッシュ (16 進数) を取得し、それを数値システムに変換して、必要な数字に切り詰めることができます。

これはあなたに役立つかもしれません: php: 数値のみのハッシュ?

于 2012-08-21T11:55:25.630 に答える
0

10^9一意の9桁の数字がありますが256^length、長さごとに文字列(ASCII文字列を想定)があります。

したがって、鳩の巣原理から、長さが4以上の文字列の場合、一意の番号を取得することはできません。(衝突が発生する必要があります)

別の方法として、従来のハッシュ関数(衝突する)を探しているか、無制限の数値を使用している可能性があります。

于 2012-08-21T11:52:04.963 に答える
0

すでに指摘したように、数値のビット数が関連付けたい文字列よりも少ない場合、「一意性」は実現できません。

あなたが探しているのは、優れたハッシュ関数です。

MD6 アルゴリズムを調べてください。カスタマイズ可能なダイジェスト長は最大 512 ビットなので、10 進数で 8 ~ 9 桁のダイジェストを作成できます。PHP の実装については知りません。元の実装言語は C です。

于 2012-08-21T12:00:42.960 に答える