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 8957331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:57:56+00:00 2026-06-15T14:57:56+00:00

Possible Duplicate: Unique random numbers in O(1)? Unique random numbers in an integer array

  • 0

Possible Duplicate:
Unique random numbers in O(1)?
Unique random numbers in an integer array in the C programming language

I have a std::vector of unique elements of some undetermined size. I want to fetch 20 unique and random elements from this vector. By ‘unique’ I mean that I do not want to fetch the same index more than once. Currently the way I do this is to call std::random_shuffle. But this requires me to shuffle the entire vector (which may contain over 1000 elements). I don’t mind mutating the vector (I prefer not to though, as I won’t need to use thread locks), but most important is that I want this to be efficient. I shouldn’t be shuffling more than I need to.

Note that I’ve looked into passing in a partial range to std::random_shuffle but it will only ever shuffle that subset of elements, which would mean that the elements outside of that range never get used!

Help is appreciated. Thank you!

Note: I’m using Visual Studio 2005, so I do not have access to C++11 features and libraries.

  • 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-06-15T14:57:57+00:00Added an answer on June 15, 2026 at 2:57 pm

    Use a loop to put random index numbers into a std::set and stop when the size() reaches 20.

    std::set<int> indexes;
    std::vector<my_vector::value_type> choices;
    int max_index = my_vector.size();
    while (indexes.size() < min(20, max_index))
    {
        int random_index = rand() % max_index;
        if (indexes.find(random_index) == indexes.end())
        {
            choices.push_back(my_vector[random_index]);
            indexes.insert(random_index);
        }
    }
    

    The random number generation is the first thing that popped into my head, feel free to use something better.

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

Sidebar

Related Questions

Possible Duplicate: Unique random numbers in O(1)? How do I fill an integer array
Possible Duplicate: Determining if an unordered vector<T> has all unique elements I have to
Possible Duplicate: Generate unique random numbers in Java I am creating a lottery app
Possible Duplicate: How to make elements of vector unique? (remove non adjacent duplicates) Remove
Possible Duplicate: Permutations - all possible sets of numbers I have an array that
Possible Duplicate: How do I generate a list of n unique random numbers in
Possible Duplicate: optimal algorithm for finding unique divisors I have asked this question before,
Possible Duplicates: Unique (non-repeating) random numbers in O(1)? How do you efficiently generate a
Possible Duplicate: Populate unique values in to VBA array from excel I need to
Possible Duplicate: Creating unique labels in Haskell I've got a datatype Person and some

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.