Random Action

For example, I have two actions :

echo '1'; // 25%  ,    .
echo '2'; // 75%  ,    .

How to make the actions performed randomly (in some percentage is acceptable)?


Answer 1, authority 100%

Then use the code

$number =  mt_rand(1,2);
if ($number==1){
echo '1';
}
if ($number==2){
echo '2';
}
__________

$number = mt_rand(1,2);– this means that we drive into the variable $numbera random number from 1to 2and using the conditions check what value is in it, if 1then display 1, if 2then 2etc. If there are many such methods, then use the construction switchand case


Answer 2

well, for example:

$random = rand(1, 100);
if($random <= 75) {
    echo '   ';
    //     75% 
}
else {
    echo '   :(';
    //   25%
}

In your case, IMHO, the most rational solution


Answer 3

If you carefully read the course of probability theory, then the probability approaches its value with large numbers of repetitions, i.e. the probability of 75% is not 3 out of 4 and not even 75 out of 100, you can safely take 7500000 out of 10000000. Now, after reading CAREFULLY the description of the rand() function and slightly optimizing the code, we get:

echo (rand(0,10000000)>7500000)?2:1;

something like this, moreover, the larger the number, the closer the distribution probability