想要根据另一个数字生成4(或更多)数字字母数字代码。尝试过使用base_convert方法,但我不想从代码中猜出这个数字。
设法生成随机的4位字母数字代码。
$numOfAlphabets = rand(0,4);
$numOfNumbers = 4 - $numOfAlphabets;
$character_set_array = array();
$character_set_array[] = array('count' => $numOfAlphabets, 'characters' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ');
$character_set_array[] = array('count' => $numOfNumbers, 'characters' => '0123456789');
$temp_array = array();
foreach ($character_set_array as $character_set)
{
for ($i = 0; $i < $character_set['count']; $i++)
{
$temp_array[] = $character_set['characters'][rand(0, strlen($character_set['characters']) - 1)];
}
}
shuffle($temp_array);
return implode('', $temp_array);
一种想法是将数字转换为二进制数,然后用固定数字对其进行异或运算,以便我可以从代码中检索数字。
还有其他想法吗?