Question:

How to return a number of random strings from a group of strings using PHP?

by  |  earlier

0 LIKES UnLike

I have a group of 200 unique strings, and I need a PHP script to return 100 random strings from that group of 200, avoiding repetition - how to do this??

 Tags:

   Report

4 ANSWERS


  1. First of all, you need to generate 100 random numbers between 0 and 199 and save them in an array. To avoid repetition, we will use a while() loop:

    $rand_numbers = array();

    /* This first loop is to get 100 numbers

    while($counter<100) {

    static $counter = 0;

    /* Now we generate the number */

    $tempnum = rand(0,199);

    /* We use a while() loop to keep generating random numbers UNTIL the number generated is not in the array $rand_numbers */

    while(!in_array( $tempnum, $rand_numbers)) {

    $tempnum = rand(0,199);

    }

    /* We add the no repeated number to the array */

    $rand_numbers[ $counter ] = $tempnum;

    $counter++;

    }

    Ok, now we get the array of strings using the random numbers of the other array:

    for($f = 0;$f<100; $f++) {

    echo $strings[ $rand_numbers[ $f ] ];

    }

    And it is done!!

    function in_array() checks for a value in an array: http://www.php.net/in_array

    Hope it works.


  2. Have you tried the array_rand function:-

    http://uk2.php.net/manual/en/function.ar...

    This returns a number of random elements from any array, however the docs don't say whether they are distinct (but I'm guessing they are) - you can run some tests to see if this is the case by creating an array of numbers 0-99 and seeing what the outputs are when run several times.

  3. I concur with MH about using the array_rand function.  

    In conjunction with using an associative array, this will solve you query.

    (associate arrays must contain a unique key)

    e.g.

    // here is an example with 5 strings

    // if you uncomment the echo statement in the loop and print_r($strings)

    // you'll notice that two entries for 'unique string 5'

    // are in our data but only 1 entry will exist in $strings.

    $uniq_strings="unique string 1\nunique string 2\nunique string 3\nunique string 4\nunique string 5\nunique string 5\nunique string 5";

    print_r( return_rand($uniq_strings, 5));

    function return_rand($uniq_strings, $num){

        $strings = Array();

        foreach ( split("\n", $uniq_strings) as $str ) {

                //echo $str."\n";

                $strings[$str]++;

        }

        //print_r($strings);

        return( array_rand($strings, $num) );

    }

    ?>

  4. The best way is to put them in an array and then:

    $a = array (strings in here);

    $b = array (); // result array

    for ($i = 0; $i < 200; $i++) {

    $r = rand(0,count($a) - 1);

    $b[] = $a[$r];

    $a = array_splice ($a, $r, 1);

    }

    That should do the trick.

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.