How to quickly randomly remove 50 non-repeating elements in an array of 100 elements

Source: Internet
Author: User
Tags shuffle
How do I quickly randomly remove 50 non-repeating elements in an array of 100 elements?


Reply to discussion (solution)

Array_rand ($a, 50);

The array is now de-weighed, and then the random fetch can be called

The array is now de-weighed, and then the random fetch can be called


The numbers in the array are inherently non-repeating, such as 1-100 values in an array of size 100, how to quickly randomly remove the 50 elements that are not duplicated? Considering the fetch to the back, such as 49, will go to fetch the data that has been taken, this probability is quite big, how to solve this problem?

1 floor positive solution, landlord to check the handbook, Array_rand

1 floor positive solution, landlord to check the handbook, Array_rand

May I ask how the Array_rand is realized?

You can do that, too.

$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;   }}

To make an empty array, Array_rand randomly takes a join array, taking the second start to determine if the removed is already in the array, successfully deposited 50 exit loops

Function swap (& $a, & $b) {    $temp = $b;    $b = $a;    $a = $temp;} $result = Array (), $SRC = Array (), for ($i = 0; $i <; $i + +) {    $src [] = $i + 1;} $arr _len = count ($src), $count = $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));p Rint_r (Json_encode ($SRC));


How's that for writing?

Didn't #1 tell you?

$a = range (1), foreach (Array_rand ($a) as $k) $r [] = $a [$k];echo join (', ', $r);
A possible result
3,5,6,7,10,12,13,15,16,19,20,23,30,31,33,35,37,38,39,44,46,49,51,53,54,59,61,63,64,65,66,69,71,72,74,75,77,78,80,82,83,87 , 88,89,91,92,96,97,98,100

$arr = Array (1,2,3,3,4,56,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 remove one or more random cells from an array. It accepts input as an input array and an optional parameter, Num_req, indicating how many units you want to remove-if not specified, the default is 1.

If you only take one, Array_rand () returns the key name of a random cell, or returns an array containing the random key name. This allows you to randomly remove the key names and values from the array.

1. First use Array_unique to get the elements that are not duplicated.
2. Using shuffle to scramble the order
3. Use Array_slice to get 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);p Rint_r ($result);
  • Related Article

    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.