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" };
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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 thanrand().So it’d be: