如何防止两个随机数的重复组合?

时间:2016-07-17 14:46:36

标签: php random

这是我生成三字组合的代码(combo_generator.php):

<?php

include "db_connect.php";

// Query database of words
$words_sql    = "SELECT * FROM words";
$words_res    = mysqli_query($db, $words_sql)or die(mysqli_error());

// Create array of words
$array  = array();

// Loop through each word and add each to the array
while($row = mysqli_fetch_array($words_res)){
     $array[] = $row['word'];
}

// Set $word1 as random word from $array
$ran_num = array_rand($array);
$word1   = $array[$ran_num];

// Remove the chosen word from the array
array_splice($array, $ran_num, 1);

// Reset the array
$array2 = $array;

// Set $word2 as random word from $array
$ran_num2 = array_rand($array2);
$word2    = $array2[$ran_num2];

// Set variables 
$word        = 'star';
$three_words = "$word.$word1.$word2";

echo $three_words;

?>

以下是我为数据库中每个星号分配一个随机三字组合的代码(test.php):

<?php

include "db_connect.php";

// Pull all stars from database
$stars_sql = "SELECT * FROM stars";
$stars_res = mysqli_query($db, $stars_sql)or die(mysqli_error());

// Loop through every star in the array
while($row = mysqli_fetch_array($stars_res)){
     // Store the star name in a variable
     $star = $row['star_name'];

     // Create a random 3-word combination
     include "combo_generator.php"; 

     // Attach the random 3-word combination to the star 
     echo $star.'&nbsp;&nbsp;&nbsp;&nbsp;'.$three_words.'<br/><br/>';
}

?>

我正在尝试以star.word1.word2的形式生成三字组合,其中word1是数据库中的随机字,而word2是数据库中的一个不同的随机字(word1!= word2)。然后将生成的每个组合分配给数据库中的星号。

如何避免重复组合?通过重复组合,我的意思是:

组合1:star.lamp.chair;组合2:star.dog.ball;组合3:star.ball.dog;组合4:star.lamp.chair

这里,组合1和组合4是相同的 - 这种重复是我想要避免的。订单并不重要,因此组合2和组合3都可以。

2 个答案:

答案 0 :(得分:2)

您可以保存已创建的组合。

创建一个全局数组(= test.php中的变量),表示所有已创建的组合:

if(@tagcount = 0) insert into @tags values (-100); 
or tags.TagID = -100;

然后,在回显新组合之前,检查它是否已经创建。如果没有,请将其添加到阵列中。如果是,请创建一个新组合。

像这样:

/*@var $createdCombinations array*/
$createdCombinations=[];

代码的其他部分可以保持不变,因此省略了它们。

答案 1 :(得分:1)

您可以使用所有可能的单词对创建一个数组,然后从该数组中进行选择和删除:

<?php

// Retrieve a list of words from the DB and build the pairs list
include "db_connect.php";

// Query database of words
$words_sql = "SELECT * FROM words";
$words_res = mysqli_query($db, $words_sql)or die(mysqli_error());

// Create array of words
$array  = array();

// Loop through each word and add each to the array
while($row = mysqli_fetch_array($words_res)){
    $array[] = $row['word'];
}

// Create all valid pairs
$pairs = array();
foreach ($array as $word1) {
    foreach ($array as $word2) {
        if ($word1 !== $word2) {
            $pairs[] = "$word1.$word2";
        }
    }
}

// Pull all stars from database
$stars_sql = "SELECT * FROM stars";
$stars_res = mysqli_query($db, $stars_sql)or die(mysqli_error());

// Loop through every star in the array
while($row = mysqli_fetch_array($stars_res)){
    // Store the star name in a variable
    $star = $row['star_name'];

    // Set $pair as random word from $pairs
    $ran_num = array_rand($pairs);
    $pair = $pairs[$ran_num];

    // ... and remove it
    array_splice($pairs, $ran_num, 1);

    // Set variables 
    $word        = 'star';
    $three_words = "$word.$pair";

    // Attach the random 3-word combination to the star 
    echo $star.'&nbsp;&nbsp;&nbsp;&nbsp;'.$three_words.'<br/><br/>';
}

?>