Picking a reward amount based on odds

Benden

maging ang maganda mamatay
Jun 4, 2010
2,286
1,482
Hello friends,

I will try to explain this the best I can.
On a webpage a user will have 3 radio buttons to choose from then hit submit, however it doesn't matter what radio button they choose as it will be random
Then once they submit radio button, I want it to choose an amount of eitherr 50, 100, or 250 to add to their account. (these are based on odds of 70% or 50, 20% for 100 10% for 250, I don't want 33.33% chance for all). Then it would set like 'access' in the DB of the user to 0
So, what I need is that code basically.

AwGIu4vN7ri2.png


Ignore values on the image, it isn't equaling 100% :)
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
If you only want certain amounts like 50/100/250, you could store the amounts in an array and then use array_rand to select a random amount
PHP:
$possible = [50, 100, 250]; // potential amounts

$random = $possible[array_rand($possible)];

echo $random; // one of the possible amounts

You could probably just check the percentage and then change the $possible array to different amounts inside the logic
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,398
962
If you only want certain amounts like 50/100/250, you could store the amounts in an array and then use array_rand to select a random amount
PHP:
$possible = [50, 100, 250]; // potential amounts

$random = $possible[array_rand($possible)];

echo $random; // one of the possible amounts

You could probably just check the percentage and then change the $possible array to different amounts inside the logic
Canadian said:
(these are based on odds of 70% or 50, 20% for 100 10% for 250, I don't want 33.33% chance for all)

Your method is 33.33% chance for any. The way @Wess described would be how you get the proper odds:

PHP:
<?php
$rand = rand(1, 100);
$amount = array("50", "100", "250");

if($rand <= '70') {
echo "You win $amount[0]";

} elseif ($rand > '70' && $rand < '90') {
    echo "You win $amount[1]";

} else {
    echo "You win $amount[2]";
}

Don't mind the sloppiness, I did it using nano. I also ran this from shell 100 times and recorded results:
Code:
for (( i=0; i<100; i++ )); do php rand.php >> rand.txt; echo $'\r' >> rand.txt & done
Results:
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
Don't understand what you mean by 33.33% chance for any, I meant you should check the percentage elsewhere then give them an amount from the array
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,398
962
Don't understand what you mean by 33.33% chance for any, I meant you should check the percentage elsewhere then give them an amount from the array
BIOS said:
If you only want certain amounts like 50/100/250, you could store the amounts in an array and then use array_rand to select a random amount
I think you should re-read what he is asking for. The code you provided:
PHP:
$random = $possible[array_rand($possible)];
That is a 33.33% chance for any of the amounts.
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
I think you should re-read what he is asking for. The code you provided:
PHP:
$random = $possible[array_rand($possible)];
That is a 33.33% chance for any of the amounts.
Oh I didn't notice the 33.33% in the thread, in that case why not use mt_rand rather than rand?

random_int would be the best method but is only available if you're running PHP7
 
Last edited:

Users who are viewing this thread

Top