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.
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_action2percent 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.