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
https://forums.phpfreaks.com/topic/197237-php-slot-machine/
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
https://forums.phpfreaks.com/topic/197237-php-slot-machine/#findComment-1035352
Share on other sites

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.