Jump to content

Trouble with rand() in IF-Clause


Marcel1993

Recommended Posts

Hey guys,

 

I don't know what to do. I've got a script used for gamble:

 

$gamble_rand = rand($number_of_tickets,$gamble_chance);
if($gamble_chance == $gamble_rand){

 

If $number_of_tickets is set by 99 and $gamble_chance is set by 100 the chance is 1:100 that the If-Clause is not true. But it seems to me that PHP is making its on "random". Independent of the values PHP is interpreting that 20 times of 100 times the correct logic is false...

 

If $number_of_tickets is set by 1 there's no big different. Its just a little bit less, but not the chance that it has to be.

 

Thanks for helping and Merry Christmas :-)

Link to comment
https://forums.phpfreaks.com/topic/222556-trouble-with-rand-in-if-clause/
Share on other sites

$gamble_rand = rand($number_of_tickets, $gamble_chance);
if($gamble_chance == $gamble_rand){

 

There is a 1/100 chance of the if statement returning true. if $gamble_chance = 1, $gamble_chance = 100.

 

yes -> 92

no -> 9908

0.92%

 

I ran it 10,000 times...see, it works. Are you sure $number_of_tickets <> 20?

If $number_of_tickets is set by 99 and $gamble_chance is set by 100 the chance is 1:100 that the If-Clause is not true.

Uh, no. There are only two numbers rand() can return in that situation: 99 and 100. Two possibilities. Not 100.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.