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

  • Home
  • SEARCH
  • 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 7927585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:22:57+00:00 2026-06-03T19:22:57+00:00

In my algorithm I have two values that I need to choose at random

  • 0

In my algorithm I have two values that I need to choose at random but each one has to be chosen a predetermined number of times.

So far my solution is to put the choices into a vector the correct number of times and then shuffle it. In C++:

// Example choices (can be any positive int)
int choice1 = 3; 
int choice2 = 4;

int number_of_choice1s = 5;
int number_of_choice2s = 1;

std::vector<int> choices;
for(int i = 0; i < number_of_choice1s; ++i) choices.push_back(choice1);
for(int i = 0; i < number_of_choice2s; ++i) choices.push_back(choice2);
std::random_shuffle(choices.begin(), choices.end());

Then I keep an iterator to choices and whenever I need a new one I increase the iterator and grab that value.

This works but it seems like there might be a more efficient way. Since I always know how many of each value I’ll use I’m wondering if there is a more algorithmic way to go about doing this, rather than just storing the values.

  • 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-03T19:22:59+00:00Added an answer on June 3, 2026 at 7:22 pm

    You are unnecessarily using so much memory. You have two variables:

    int number_of_choice1s = 5;
    int number_of_choice2s = 1;
    

    Now simply randomize:

    int result = rand() % (number_of_choice1s + number_of_choice2s);
    if(result < number_of_choice1s) {
      --number_of_choice1s;
      return choice1;
    } else {
      --number_of_choice2s;
      return choice2;
    }
    

    This scales very well two millions of random invocations.

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

Sidebar

Related Questions

I have a relatively simple algorithm that walks an std::vector looking for two neighbouring
Given an array that has the numbers {0......2^k -1} except for one number ,
I need to merge two doubly-linked lists, but not by their values (the lists
We have a pricing dataset that changes the contained values or the number of
I have two tables, which I need to join by one column and is
I have an algorithm that recursively makes change in the following manner: public static
I have this algorithm that I want to implement on VB6. Sub Main() dim
I have simple algorithm that clean the whitespace from half string until end. Here
If I have an algorithm that takes n log n steps (e.g. heapsort), where
I made an algorithm to generate sudokus, but it was terribly inefficient. Each puzzle

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.