Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7087481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:40:46+00:00 2026-05-28T07:40:46+00:00

I have an array, A = [a1,a2,a3,…aP] with size P . I have to

  • 0

I have an array, A = [a1,a2,a3,...aP] with size P. I have to sample q elements from array A.

I plan to use a loop with q iterations, and randomly pick a element from A at each iteration. But how can I make sure that the picked number will be different at each iteration?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:40:47+00:00Added an answer on May 28, 2026 at 7:40 am

    The other answers all involve shuffling the array, which is O(n).
    It means modifying the original array (destructive) or copying the original array (memory intensive).

    The first way to make it more memory efficient is not to shuffle the original array but to shuffle an array of indexes.

    # Shuffled list of indexes into @deck
    my @shuffled_indexes = shuffle(0..$#deck);
    
    # Get just N of them.
    my @pick_indexes = @shuffled_indexes[ 0 .. $num_picks - 1 ];  
    
    # Pick cards from @deck
    my @picks = @deck[ @pick_indexes ];
    

    It is at least independent of the content of the @deck, but its still O(nlogn) performance and O(n) memory.

    A more efficient algorithm (not necessarily faster, depends on now big your array is) is to look at each element of the array and decide if it’s going to make it into the array. This is similar to how you select a random line from a file without reading the whole file into memory, each line has a 1/N chance of being picked where N is the line number. So the first line has a 1/1 chance (it’s always picked). The next has a 1/2. Then 1/3 and so on. Each pick will overwrite the previous pick. This results in each line having a 1/total_lines chance.

    You can work it out for yourself. A one line file has a 1/1 chance so the first one is always picked. A two line file… the first line has a 1/1 then a 1/2 chance of surviving, which is 1/2, and the second line has a 1/2 chance. For a three line file… the first line has a 1/1 chance of being picked, then a 1/2 * 2/3 chance of surviving which is 2/6 or 1/3. And so on.

    The algorithm is O(n) for speed, it iterates through an unordered array once, and does not consume any more memory than is needed to store the picks.

    With a little modification, this works for multiple picks. Instead of a 1/$position chance, it’s $picks_left / $position. Each time a pick is successful, you decrement $picks_left. You work from the high position to the low one. Unlike before, you don’t overwrite.

    my $picks_left = $picks;
    my $num_left = @$deck;
    my @picks;
    my $idx = 0;
    while($picks_left > 0 ) {  # when we have all our picks, stop
        # random number from 0..$num_left-1
        my $rand = int(rand($num_left));
    
        # pick successful
        if( $rand < $picks_left ) {
            push @picks, $deck->[$idx];
            $picks_left--;
        }
    
        $num_left--;
        $idx++;
    }
    

    This is how perl5i implements its pick method (coming next release).

    To understand viscerally why this works, take the example of picking 2 from a 4 element list. Each should have a 1/2 chance of being picked.

    1. (2 picks, 4 items):         2/4 = 1/2
    

    Simple enough. Next element has a 1/2 chance that an element will already have been picked, in which case it’s chances are 1/3. Otherwise its chances are 2/3. Doing the math…

    2. (1 or 2 picks,  3 items):   (1/3 * 1/2) + (2/3 * 1/2) = 3/6 = 1/2
    

    Next has a 1/4 chance that both elements will already be picked (1/2 * 1/2), then it has no chance; 1/2 chance that only one will be picked, then it has 1/2; and the remaining 1/4 that no items will be picked in which case it’s 2/2.

    3. (0, 1 or 2 picks, 2 items): (0/2 * 1/4) + (1/2 * 2/4) + (2/2 * 1/4) = 2/8 + 1/4 = 1/2
    

    Finally, for the last item, there’s a 1/2 the previous took the last pick.

    4. (0 or 1 pick, 1 items):     (0/1 * 2/4) + (1/1 * 2/4) = 1/2
    

    Not exactly a proof, but good for convincing yourself it works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have array of countries. I want to pick 5 random countries from my
An array is defined of assumed elements like I have array like String[] strArray
VISUAL C++ Question Hi, I have array of 3 elements and I want to
I have array that element is hash a = [{:history_date=>15/07/10}, {:open_price=>7.90}] I want to
I have: $overr=array(); $overr[]=array(selector=>array('vi'=>mysql_num_rows($myquery),'pes'=> $pess,'prp'=>mysql_num_rows($my_3_query),'em_t'=>$u_h));//this is in a loop As you can see, I'm
I have array of objects. I want to write method wich i will use
I have array of objects MyClass MyArr[10]; In method of one element of array
I want to enumerate all possible combinations of elements array. For example: I have
I have array number = {2,3,4,5,6} Now i have to select rows from table
Imagine I have array of values ranging from 1 to 24. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.