今天我的朋友提出了一个挑战,我仍然无法解决:“用PHP生成随机数字序列”
这些数字排列成拨号键盘/图案锁,由3行3列的1-9个数字键组成:
---------------------------
| |
| 1 2 3 |
| |
| 4 5 6 |
| |
| 7 8 9 |
| |
---------------------------
现在,我们需要生成一个指定长度的随机、不重复数字序列,需要遵守以下规则:
1. 序列中的数字不能重复
2. 生成的数字序列必须是随机的
3. 数字序列的长度由参数指定
A generated sequence should follow a specific direction/pattern going only via neighboring digits (possibly diagonally), for example (length:8), 12569874:
1 2 4 5 6 7 8 9
Digits from the first row should never be followed by a digit from the third row, and vice-versa. The same goes for columns. For example a 1 cannot be followed by a 8, and a 6 cannot be followed by a 4.
can guess more criteria can easily from android pattern-lock system
$chars = "123456789";
$length = 9;
$clen = strlen( $chars )-1;
$id = '';
for ($i = 0; $i < $length; $i++) {
$id .= $chars[mt_rand(0,$clen)];
}
return ($id);
但是,仍然没有运气...
我该如何解决这个问题?