jeudi 21 juillet 2016

How can I avoid duplicate random three-word combinations from a table of words?

I am trying to pull a list of words from the database, create a unique three-word combination in the form $word1.$word2.$word3, and assign it to a star.

I want to avoid duplicate combinations - I want each star to have a unique three-word identifier.

My current method involves creating an array of all possible three-word combinations and then deleting each combination from the array once it has been assigned to a star. I intend, however, to use a few-thousand words in my word list, which means this array will contain tens of billions of combinations, so this method seems incredibly inefficient.

How can I achieve this more effectively? My initial thoughts are that I should loop through each star, create and assign a three-word combination, then add the combo to an array, and for each star, check if the newly generated combo is in the array.

Code

 <?php

    // Initiate connection to the database...
    $db = mysqli_connect('localhost', 'root', '', 'stellar');

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

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

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

    // Create array of all possible three-word combinations, from which we will randomly select our combinations 
    $triplets = array();
    foreach ($words as $word1){
        foreach ($words as $word2){
            foreach($words as $word3){
                if ($word1 !== $word2 && $word2 !== $word3 && $word1 !== $word3){
                     $triplets[] = "$word1.$word2.$word3";
                }
            }    
        }
    }

    // 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 and star_id in variables
         $star    = $row['star_name'];
         $star_id = $row['star_id'];

         // Set $three_words as a random combination from the array of possible combinations...
         $ran_num     = array_rand($triplets);
         $three_words = $triplets[$ran_num];

         // ...and remove this particular combination, in order to prevent repating combinations
         array_splice($triplets, $ran_num, 1);

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




Aucun commentaire:

Enregistrer un commentaire