Jump to content

PHP gambling with mt_rand


sw0o0sh

Recommended Posts

Hi, I run a gameserver that has it's own type of currency, and I'm making a gamble system where people can wager their "game gold". All they do is bet the amount of gold they wish to bet, and the script does the rest. Say they have a 1 in 15 chance of doubling their wager, is this appropriate (as in, is this really a 1 in 15 chance?).

 

$generated_number = mt_rand(1,15);

$selected_number = mt_rand(1,15);

 

if($generated_number == $selected_number) { you won }

 

etc etc

 

Is that really a 1 in 15 chance? I'm not that swell with math, and is there a term for what I'm doing so I could perhaps learn?

Link to comment
Share on other sites

That looks like 1 in 15 to me.  This is also 1 in 15:

 

$selected_number = mt_rand(1,15);
if ($selected_number == 1) { you won }

 

1 in 15 is not very good odds if they lose their entire wager when they lose.  You could do something like 45 out of 100:

 

$selected_number = mt_rand(1,100);
if ($selected_number <= 45) { you won }

 

Then they will generally lose in the long run, but it's also likely for them to have lucky streaks.

Link to comment
Share on other sites

That looks like 1 in 15 to me.  This is also 1 in 15:

 

$selected_number = mt_rand(1,15);
if ($selected_number == 1) { you won }

 

1 in 15 is not very good odds if they lose their entire wager when they lose.  You could do something like 45 out of 100:

 

$selected_number = mt_rand(1,100);
if ($selected_number <= 45) { you won }

 

Then they will generally lose in the long run, but it's also likely for them to have lucky streaks.

 

Thanks, that first way seems a bit more efficient. One last question though,

say they have a 1 in 10 chance to win their bet x 2,

1 in 100 chance to win their bet x 10,

1 in 1000 chance to win their bet x 100, etc etc,

 

How would I figure there total chance of winning anything (as in, any 3 of those possibilities)?

Link to comment
Share on other sites

I wouldn't worry about efficiency here.  More important is how clear the code is.  If you want to calculate the probability of any of a number of random events happening, the formula is:

 

Let P1 = 0.1, P2 = 0.01, P3 = 0.001 (probabilities of winning x2, x10, x100).

The chance of NOT getting ALL of those is (1-P1) * (1-P2) * (1-P3).

The inverse of that is the chance of getting AT LEAST ONE of those, 1 - (1-P1) * (1-P2) * (1-P3)

= 1 - (0.890109) = 0.109891

 

That's just under 11%

 

That doesn't take into account how much they win each time though.  Calculating their likely winnings is another matter altogether, complicated by the fact that their winnings on the next round are affected by how much they won/lost the previous round.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.