How can I randomly execute code 33% of the time in PHP?
php, random
Solution
The two arguments represent the minimum and maximum random values. If you want a 1-in-3 chance, you should only allow 3 possibilities. Going from minimum 0 to maximum 3 allows 4 possible values (0,1,2,3), so that won't quite do what you want. Also, `mt_rand()` is a better function to use than `rand()`.
So it'd be:
if (mt_rand(1, 3) == 2)
echo "Success";
Problem
I'm not so sure how to write this: I want to generate a random number from 0 to 2, then write an `if` statement which executes specific code only 33% of the time. This is what I tried to do: ``` if (rand(0, 3)=="2") { echo "Success" }; ```