Porl123 Posted April 1, 2010 Share Posted April 1, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/197237-php-slot-machine/ Share on other sites More sharing options...
ignace Posted April 1, 2010 Share Posted April 1, 2010 $array = array('7', 'bar', 'orange', 'cherry', 'MISS'); print_r(array_rand($array, 3)); Quote Link to comment https://forums.phpfreaks.com/topic/197237-php-slot-machine/#findComment-1035327 Share on other sites More sharing options...
Porl123 Posted April 1, 2010 Author Share Posted April 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/197237-php-slot-machine/#findComment-1035332 Share on other sites More sharing options...
andrewgauger Posted April 1, 2010 Share Posted April 1, 2010 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"; Quote Link to comment https://forums.phpfreaks.com/topic/197237-php-slot-machine/#findComment-1035352 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.