Jump to content

php slot machine


Porl123

Recommended Posts

Hey guys, I was just trying to make a simple php slot machine with realistic probability and was wondering whether anyone would have an idea of how to do that?

The slot machine has 3 reels. I've started a function which produces the reels and has the option to select from '7', 'bar', 'orange', 'cherry', 'MISS (spun between 7 and cherry)' and 'MISS (spun between cherry and orange)'.

If anyone can direct me on how to weight this probability to make the outcomes such as 7 and bar appear less often I'd be very appreciative. Thanks in advance!

Link to comment
Share on other sites

Yeah, that's pretty much what I've got at the moment, except it only returns one reel per call to the function. What I was wondering is whether it could have weighted probability, so it chooses the misses and cherries than for example, the 7.

Link to comment
Share on other sites

After the 12th line of code in crafting something for you, I decided there had to be a more efficient way.  Check this out:

http://20bits.com/articles/random-weighted-elements-in-php/

 

function w_rand($weights) {
    $r = mt_rand(1,1000);
    $offset = 0;
    foreach ($weights as $k => $w) {
        $offset += $w*1000;
        if ($r <= $offset) {
            return $k;
        }
    }
}

 

so to start you have:

a=array('7', 'bar', 'orange', 'cherry', 'MISS', 'MISS2');
w=array(.05,.20,.30,.25,.10,.10);

echo a[w_rand(w)];

should get you what you want.  Remember that:

if(array_sum(w) != 1)
echo "results will not behave as expected";

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.