How to quickly randomly retrieve 50 non-repeated elements from an array of 100 elements-php Tutorial

Source: Internet
Author: User
Tags shuffle
How to quickly randomly retrieve 50 non-repeated elements from an array of 100 elements how to quickly randomly retrieve 50 non-repeated elements from an array of 100 elements?


Reply to discussion (solution)

Array_rand ($ a, 50 );

Remove the array and then call random fetch.

Remove the array and then call random fetch.


The numbers in the array are not repeated. for example, if one to 100 values are placed in an array with a size of, how can we quickly randomly retrieve 50 elements that are not repeated? Considering that when we get the following data, for example, when we get 49 data records, the probability of getting the data is quite high. what can we do to solve this problem?

On the first floor, check the manual. array_rand

On the first floor, check the manual. array_rand

How is array_rand implemented?

You can also achieve this,

$num =0;$array = array(1,2,3,4,5,6,7,8,9);$arr =array();$g    = 5;$tag = true;while($tag){   $count = count($array);   $t = rand(0,1);    if($t==1){        $arr[] = $array[$num];        unset($array[$num]);    }   $num++;   if(count($arr)==$g){    $tag=false;   }   if($num == $count){     $num  = 0;   }}

Create an empty array, array_rand randomly fetch an array, take the second one, and start to judge whether it is already in the array. then, 50 entries are successfully saved to exit the loop.

function swap(&$a, &$b){    $temp = $b;    $b = $a;    $a = $temp;}$result = array();$src = array();for($i = 0 ; $i < 40 ; $i++){    $src[] = $i + 1;}$arr_len = count($src);$count = 20;$index = 0;while($index < $count){    $random = rand(0, $arr_len - $index - 1);    $result[] = $src[$random];    swap($src[$random] , $src[$arr_len - $index - 1]);    $index += 1;}print_r(json_encode($result));print_r(json_encode($src));


How is this written?

#1 didn't I tell you?

$a = range(1, 100);foreach(array_rand($a, 50) as $k) $r[] = $a[$k];echo join(',', $r);
A possible result
, 98,100, 87

$ Arr = array (, fdas ,....);
$ Unique = array_unique ($ arr );
Print_r (array_rand ($ unique, 50 ));

Mixed array_rand (array input [, int num_req])


Array_rand () is useful when you want to extract one or more random units from an array. It accepts input as an input array and an optional parameter num_req, specifying the number of units you want to retrieve-if not specified, the default value is 1.

If you only retrieve one key, array_rand () returns the key name of a random unit. Otherwise, an array containing the random key name is returned. In this way, you can randomly retrieve the key name and value from the array.

1. use array_unique to obtain non-repeated elements.
2. use shuffle to disrupt the order
3. use array_slice to obtain 50 elements.

Example:

$arr = array(1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8);$arr_uni = array_unique($arr);shuffle($arr_uni);$result = array_slice($arr_uni, 0, 5);print_r($result);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.