4
$array = array(1, 2, 3, 4, 5, ..., N);

还有一个号码D = 10%。以这种方式对数组进行排序的最快方法是:

$sorted_array = {a[i]} 

$array完全包含混合顺序的元素,但还包含:

abs(a[i + 1] - a[i]) >= N * 10% 

对于任何人[i],并尽可能地随机化。

例如,

// assume D = 25%
$array = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

// so the difference between any neighbors is >= 4 = 10 * 25%.
$sorted_array = array(4, 8, 3, 7, 1, 5, 9, 2, 6, 10);

当然如果D很大,就不可能对我想要的数组进行排序。我不需要 100% 完美的结果,但我希望这些数字看起来是“随机的”,并且大多数数字至少有 10% 的差异。

我有一个奇怪的任务,但它有一个实用的区域可以使用。我想从图像中提取随机线,它们应该尽可能不同。当然,数字图像(照片等)上的相邻线看起来非常相似。

我解释得当吗?

4

1 回答 1

2

我知道仅仅提供代码不是一个好主意,但我对这个问题很感兴趣。这是我的做法:

$d = 0.3;
$random = array();

// Populate the original array
for ($n=1; $n <= 10; $n++) {
    $arr[] = $n;
}

$count = count($arr);

// Loop through array
foreach (array_keys($arr) as $key) {
    if (!isset($prev_key)) {
        $prev_key = array_rand($arr);
    }
    $possibles = array(); // This stores the possible values
    echo "Trying: $prev_key";
    echo ":\n";

    // Loop through the array again and populate $possibles with all possible
    // values based on the previous values
    foreach (array_keys($arr) as $n) {
        if ($arr[$n] < $prev_key - $count * $d || $arr[$n] > $prev_key + $count * $d) {
            $possibles[] = $n;
            echo $arr[$n]." is valid\n";
        }
        else {
            echo $arr[$n];
            echo " outside range\n";
        }
    }

    // If there is nothing outside that range, just return the remaining values
    if (count($possibles) == 0) {
        $possibles = array_keys($arr);
        echo "Nothing within range so just returning whole array\n";
    }
    echo "\n";

    // Choose random value from the possible values array
    $rand_key = $possibles[array_rand($possibles)];

    $random[] = $arr[$rand_key];
    $prev_key = $arr[$rand_key];

    // Unset this value from the original array since we can only use the
    // values once
    unset($arr[$rand_key]);
}

print_r($random);

这将产生如下输出:

Trying: 8:
1 is valid
2 is valid
3 is valid
4 is valid
5 outside range
6 outside range
7 outside range
8 outside range
9 outside range
10 outside range

Trying: 2:
1 outside range
3 outside range
4 outside range
5 outside range
6 is valid
7 is valid
8 is valid
9 is valid
10 is valid

Trying: 9:
1 is valid
3 is valid
4 is valid
5 is valid
6 outside range
7 outside range
8 outside range
10 outside range

Trying: 5:
1 is valid
3 outside range
4 outside range
6 outside range
7 outside range
8 outside range
10 is valid

Trying: 10:
1 is valid
3 is valid
4 is valid
6 is valid
7 outside range
8 outside range

Trying: 4:
1 outside range
3 outside range
6 outside range
7 outside range
8 is valid

Trying: 8:
1 is valid
3 is valid
6 outside range
7 outside range

Trying: 3:
1 outside range
6 outside range
7 is valid

Trying: 7:
1 is valid
6 outside range

Trying: 1:
6 is valid

Array
(
    [0] => 2
    [1] => 9
    [2] => 5
    [3] => 10
    [4] => 4
    [5] => 8
    [6] => 3
    [7] => 7
    [8] => 1
    [9] => 6
)

唯一的缺点是,由于它随机获取行,因此接近末尾的值可能不会超出定义的范围。根据我的测试,使用上述$d = 0.25值和 1000 个值的情况大约为 4%。解决此问题的一种方法是将这些值重新插入到随机位置,而不是像我所做的那样附加它们。

另请注意,此方法效率不高。它必须遍历数组count($arr) ^ 2时间。因此,对于 1000 个值,您正在查看 1,000,000 次迭代。幸运的是,数组逐渐变小。

于 2013-08-05T21:40:46.487 回答