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.

abrahab
  • 2,430
  • 9
  • 39
  • 64
  • 1
    You've given us your current model, but you haven't told us what you're modeling. What are these actions? [What is your goal](http://www.catb.org/esr/faqs/smart-questions.html#goal)? – GManNickG May 22 '12 at 23:21
  • Is this homework? Consider tagging as such. This might lead to people having a little bit more patience. I find this question unintelligible right now – sehe May 22 '12 at 23:22
  • So you want to guarantee that after they randomly get one, that the next button press will give the other? – Mooing Duck May 22 '12 at 23:23
  • @GManNickG Goal is to avoid excessive number of repeating actions that rund() can give. for example with `percent = 50` rand() can give 10/10 action2! – abrahab May 22 '12 at 23:25
  • @abrahab: What would you like instead? 5/10 every single time? That's harder to do. – Mooing Duck May 22 '12 at 23:26
  • @MooingDuck the simplified logic if `percent = 50` but if its less, I think only need to reduce the likelihood of action that already showed. – abrahab May 22 '12 at 23:27
  • @sehe no, its not. I know only php and cpp. and wrote this on cpp. threfore, its easy to understand examples on familiar lang. – abrahab May 22 '12 at 23:28
  • 2
    What is the actual requirement for the distribution of results? Its clearly not random, since random numbers have no memory and you can get action1 every time and thats still good because the next time it might be action2. Decide what you want first. Then coding it is easier. – Julian May 22 '12 at 23:40
  • @Julian Seems for users its no good to get the same action each time if `percent` suggests others. – abrahab May 22 '12 at 23:51
  • 1
    So what is good? Your specification is currently contradictory and you can't deliver against it. You need a specification that defines the acceptable outcomes. Take a look here http://stackoverflow.com/q/2060774/1002025 for a similar discussion. – Julian May 23 '12 at 16:08

2 Answers2

1
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%
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
0

If you don't want actions repeated, you can either a) choose from all BUT the last action, or b) choose as you do now, but keep re-choosing until you get something other than the previous action. The latter is easier to do, but is slower (possibly a LOT slower).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101