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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:39:11+00:00 2026-06-04T09:39:11+00:00

We have button. User clicking the button and receive action1 or action2 depended by

  • 0

We have button. User clicking the button and receive action1 or action2 depended by variable percent_to_action2(from 0 to 100). The simplest way to give him action1 or action2 is based on rand() % 100 and comparison with percent_to_action2.

But the problem is that if eg. perfect_to_action = 50 there is no guaranty that after first random action1 user will get action2 (by rand()). I am searching the ways to avoid many repeating actions. Please, suggest how to count more accurately considering the previous event/or all events. with examples and comments. Goal is to avoid excessive number of repeating actions that rund() can give. for example with percent = 50 rand() can give 10/10 action2!

ps. perfect_to_action can be changed at any time.
pps. sorry for me english.

my code:

                int num_rand = (rand() % 100 ) + 1; // from 1 to 100
                if (  num_rand <= current_percent_to_action2 )
                {
                    // action 1
                } else {} // action2

What I want in examples:

percent = 50:
action1 than action2 than action1 than action2 etc.

percent = 33:
(first by rand)
if first action1 than action1 than action2 than action1 than action1 than action2 etc.

  • 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-04T09:39:13+00:00Added an answer on June 4, 2026 at 9:39 am
    static unsigned num_action_1 = 1;
    static unsigned num_action_2 = 1;
    double bias = double(num_action_2)/num_action_1;
    double randomchance = 1.0-current_percent_to_action2/100.0;
    double action_1_cutoff = RAND_MAX*randomchance*bias;
    if (  rand() <= action_1_cutoff ) {
        // action 1
        ++num_action_1;
    } else {
        // action2
        ++num_action_2;
    } 
    

    This will bias the randomness toward the option that has happened less frequently. I also changed it so that action 2 will happen roughly current_percent_to_action2 percent of the time, instead of action 1 like in your code. As you can see by this chart, it adds a lot of complexity, but you are far less likely to get unbalanced numbers of results. In the long term, these will end up virtually identical though, both will give strings of 10 in a row eventually, this code just starts far more even.

    times #1     Even distribution    Biased distribution
        1               50%                  50%
        2               25%                   8.3%
        3               12.5%                 3.125%
        4                6.25%                1.25%
        5                3.13%                0.52%
        6                1.56%                0.22%
        7                0.78%                0.09%
        8                0.39%                0.04%
        9                0.20%                0.02%
        10               0.10%                0.01%
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem passing a variable from a main page containing a user control
I have a button and image inside it in my silverlight user control. When
I have a button and an edittext. The user enters a number and then
I have a button and a spinner (originally hidden). When user presses a button,
I have a button which when pressed, sets the user's rights in the db.
In my project i have a button. when user clicks on it, it shows
I have a submit button that changes when the user hovers his mouse over
I have a Table,Button and a Textarea, When a user types inside the Textarea
I have 2 radio button with values New & Existing. If user chooses New,
I have a radio button with values, Yes and No. If user selects Yes,

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.