Cara menggunakan php randomize array

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    There are two functions to get random value out of an array in PHP. The shuffle() and array_rand() function is used to get random value out of an array.

    Examples: 

    Input : $arr = ("a"=>"21", "b"=>"31", "c"=>"7", "d"=>"20")
    
    // Get one random value
    Output :7
    
    Input : $arr = ("a"=>"21", "b"=>"31", "c"=>"7", "d"=>"20")
    
    // Get two random values
    Output : 21 31

    Method 1: This method discuss the shuffle() function to get random value out of an array in PHP.
    PHP | shuffle() Function: The shuffle() Function is an inbuilt function in PHP which is used to shuffle or randomize the order of the elements in an array. This function assigns new keys for the elements in the array. It will also remove any existing keys, rather than just reordering the keys and assigns numeric keys starting from zero.

    Syntax:  

    bool shuffle( $array )

    Example:  

    PHP

    $arr = array( "a"=>"21", "b"=>"31", "c"=>"7", "d"=>"20" );

    shuffle($arr);

    echo $arr[0];

    ?>

    In the above example the keys of associative array have been changed. The shuffle() function has randomly assigned keys to elements starting from zero. As shuffle() permanently change the keys of an array.

    Method 2: Use array_rand() function to get random value out of an array in PHP.
    PHP | array_rand() Function: The array_rand() function is an inbuilt function in PHP which is used to fetch a random number of elements from an array. The element is a key and can return one or more than one key.

    Syntax: 

    array_rand( $array, $num )

    This function accepts two parameters $array and $num. The $array variable store the array elements and $num parameter holds the number of elements need to fetch. By default value of this parameter is 1.

    Example 1:  

    PHP

    $arr = array( "a"=>"21", "b"=>"31", "c"=>"7", "d"=>"20" );

    $key = array_rand($arr);

    echo $arr[$key];

    ?>

    In the above example we didn’t explicitly specified the value for second parameter so by default the value is 1 and array_rand() will return one random key.

    Example 2: This example explicitly specify the value for second parameter so array_rand() function will return the array of random keys. 

    PHP

    $arr = array( "a"=>"21", "b"=>"31", "c"=>"7", "d"=>"20" );

    $num = 2;

    $keys = array_rand( $arr, $num );

    echo $arr[$keys[0]]." ".$arr[$keys[1]];

    ?>


    Whether it is Web application, WAP or mobile application, random numbers have their applications. In the recent contact with several small projects, I often need to deal with random numbers or random arrays, so for PHP how to generate non-repeating random numbers commonly used in a summary of several methods (ps: methods 1, 4 and 5 are commonly used by me, and the rest come from network collation)

    Table of Contents

    • Questions : PHP Random Array with no duplicates
    • Answers 1 : of PHP Random Array with no duplicates
    • How do you generate a non repeating random number in PHP?
    • How do you make a random number not repeat?
    • How to pick random value from array in PHP?

    Method 1:


    $numbers = range (1,50);
    //shuffle Scramble the array order immediately
    shuffle ($numbers);
    //array_slice Fetches a certain in the array 1 Segment
    $num=6;
    $result = array_slice($numbers,0,$num);
    print_r($result);
    ?>

    Method 2:


    $numbers = range (1,20);
    // Sow the seeds of random number generator, optional, and have no effect on the results after testing
    srand ((float)microtime()*1000000);
    shuffle ($numbers);
    // Skip list No. 1 1 Values (the index is saved)
    while (list(, $number) = each ($numbers)) {
    echo "$number ";
    }
    ?>

    Method 3:


    function NoRand($begin=0,$end=20,$limit=5){
    $rand_array=range($begin,$end);
    shuffle($rand_array);// Call the ready-made array random arrangement function
    return array_slice($rand_array,0,$limit);// Before interception $limit A
    }
    print_r(NoRand());
    ?>

    The above can randomly generate 5 non-repeating values between 1 and 20

    Method 4:


    $tmp=array();
    while(count($tmp)<5){
    $tmp[]=mt_rand(1,20);
    $tmp=array_unique($tmp);
    }
    print_r($tmp);
    ?>

    Method 5:


    $tmp = range(1,30);
    print_r(array_rand($tmp,10));
    ?>

    This may be simpler than it is called (ps: If you specify a step size in range, you must pay attention to whether the second parameter of array_rand exceeds the length of $tmp).

    PHP provides a very rich array function, most random numbers can be generated from the perspective of arrays, if you have methods to provide, welcome to give, the article will continue to update.

    Questions : PHP Random Array with no duplicates

    2022-09-19T01:40:00+00:00 2022-09-19T01:40:00+00:00

    606

    I'm trying to get something right when I anycodings_php reload the browser the array changes anycodings_php randomly with no duplicate. When I reload anycodings_php the browser I saw duplicate array coming anycodings_php randomly, please help me.

    Here is the code

    
        
        
            
    " . $fitness1[array_rand($fitness1)] . "
    ";echo $number++ . "
    " . $fitness1[array_rand($fitness1)] . "
    "; for ($i = 0; $i < $size; $i++){ } } ?>

    Total Answers 1

    29

    Answers 1 : of PHP Random Array with no duplicates

    $i = 0;
    $number = 1;
    do {
        shuffle($fitness1);
        echo "" . ($number++) ."" . array_shift($fitness1) ."";
        echo "" . ($number++) ."" . array_shift($fitness1) ."";
        $i++;
    } while ($i < $size && !empty($fitness1));
    

    shuffle() This function shuffles anycodings_php (randomizes the order of the elements anycodings_php in) an array Blockquote

    So each time you reload the page, the anycodings_php order will be different

    https://www.php.net/manual/en/function.shuffle.php anycodings_php

    array_shift() shifts the first value of anycodings_php the array off and returns it, shortening anycodings_php the array by one.

    https://www.php.net/manual/en/function.array-shift.php

    Once the value is printed, it will be anycodings_php removed from the array. So no repeated anycodings_php values

    0

    2022-09-19T01:40:00+00:00 2022-09-19T01:40:00+00:00Answer Link

    mRahman

    How do you generate a non repeating random number in PHP?

    php $check = array(); function generateNumber() { global $check; $page_no = mt_rand(1,20); $check[] = $page_no; if (count($check) !=

    How do you make a random number not repeat?

    Generate Random Number List With No Duplicates in Excel.

    Select cell B3 and click on it..

    Insert the formula: =RANDBETWEEN(10,30).

    Press enter..

    Drag the formula down to the other cells in the column by clicking and dragging the little “+” icon at the bottom-right of the cell..

    How to pick random value from array in PHP?

    The array_rand() function returns a random key from an array, or it returns an array of random keys if you specify that the function should return more than one key.