1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Probability in PHP

Discussion in 'PHP' started by LongshotMP, Apr 18, 2017.

  1. #1
    Hi there,

    I was wondering if someone could help. I am in the process of creating a game, whereby you can steal cars. I have put together a script where as you can see there are a range from "car 1" (being the nicest car) to "car 12" which is an old crappy car. Now, I have set the probability from 1,100 and so the top 2 cars are set at 99.... which I thought would therefore make it difficult to steal these. Is this correct? As it seems quite easy to steal just any car at the moment...

    $random_car = rand (1,100);
    
    
    if (($random_car)>99)
    {$car = $car_1; $pic = $car1_pic;}
    else
    {
    if (($random_car)>99)
    {$car = $car_2; $pic = $car2_pic;}
    else
    {
    if (($random_car)>90)
    {$car = $car_3; $pic = $car3_pic;}
    else
    {
    if (($random_car)>80)
    {$car = $car_4; $pic = $car4_pic;}
    else
    {
    if (($random_car)>70)
    {$car = $car_5; $pic = $car5_pic;}
    else
    {
    if (($random_car)>60)
    {$car = $car_6; $pic = $car6_pic;}
    else
    {
    if (($random_car)>50)
    {$car = $car_7; $pic = $car7_pic;}
    else
    {
    if (($random_car)>40)
    {$car = $car_8; $pic = $car8_pic;}
    else
    {
    if (($random_car)>35)
    {$car = $car_9; $pic = $car9_pic;}
    else
    {
    if (($random_car)>25)
    {$car = $car_10; $pic = $car10_pic;}
    else
    {
    if (($random_car)>20)
    {$car = $car_11; $pic = $car11_pic;}
    else
    {
    if (($random_car)>5)
    {$car = $car_12; $pic = $car12_pic;}
    else
    {
    PHP:
     
    Solved! View solution.
    Last edited by a moderator: Apr 20, 2017
    LongshotMP, Apr 18, 2017 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    I dunno about that, but... why have you created the if/else the way you have? Wouldn't it be a lot easier to just do if / elseif / else?

    Currently you have a LOT of nested if / else / if:

    
    if (($random_car)>99) {
       $car = $car_1; $pic = $car1_pic;
    } else {
       if (($random_car)>99) {
         $car = $car_2; $pic = $car2_pic;
       } else {
         if (($random_car)>90) {
           $car = $car_3; $pic = $car3_pic;
         } else {
           if (($random_car)>80) {
             $car = $car_4; $pic = $car4_pic;
           } else {
             if (($random_car)>70) {
               $car = $car_5; $pic = $car5_pic;
             } else {
               if (($random_car)>60) {
                 $car = $car_6; $pic = $car6_pic;
               } else {
                 if (($random_car)>50) {
                   $car = $car_7; $pic = $car7_pic;
                 } else {
                   if (($random_car)>40) {
                     $car = $car_8; $pic = $car8_pic;
                   } else {
                     if (($random_car)>35) {
                       $car = $car_9; $pic = $car9_pic;
                     } else {
                       if (($random_car)>25) {
                         $car = $car_10; $pic = $car10_pic;
                       } else {
                         if (($random_car)>20) {
                           $car = $car_11; $pic = $car11_pic;
                         } else {
                           if (($random_car)>5) {
                             $car = $car_12; $pic = $car12_pic;
                         } else {
    
    PHP:
    (proper formatting helps a lot...)
     
    PoPSiCLe, Apr 18, 2017 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    What do you want help with? Fixing this code, or a better way of stealing car 1 ;)
    Also, what is the difference between first two statements. Only car one will get selected each time...
    And you should not use so many if else statements. Will end up confusing yourself at the end...

    You can use arrays and pick a car from the array based on RAND result. Sample:

    $cars= array( 'empty0', 'pic1.jpg', 'pic2.jpg', 'pic3', 'pic4', 'pic5', 'pic6', 'pic7', 'pic8', 'pic9', 'pic10', 'pic11', 'pic12' );
    $rand = rand( 0, 120 );
    $selectedCar = ceil( $rand/10 );
    $pic = $cars[$selected_car];

    So if random number is 116, you'll get 12th car 116/10 = ceil( 11.6 ) = 12
    if random number is 112, you'll still get 12th car 112/10 = ceil( 11.2 ) = 12

    if random number is 96, you'll get 10th car 96/10 = ceil( 9.6 ) = 10

    if random number is 6, you'll get car1 6/10 = ceil( 0.6 ) = 1


    Here probability is same for all cars. You can make it difficult to select car one like this:

    if( $selectedCar == 1 && rand(0,10)<5 ){ $selectedCar=2; }

    // basically a second rand check
     
    Last edited: Apr 20, 2017
    JEET, Apr 20, 2017 IP
    matt_62, ThePHPMaster and sarahk like this.
  4. LongshotMP

    LongshotMP Peon

    Messages:
    9
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #4

    Thank you Jeet, very helpful! It is a bit confusing at I'm not used to this, although I will try it and let you know how it goes.
     
    LongshotMP, Apr 21, 2017 IP
    JEET likes this.
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    If the first two are both set to >99, the second one would NEVER fire as the first one would ALWAYS be the one handled.
     
    deathshadow, Apr 26, 2017 IP
  6. LongshotMP

    LongshotMP Peon

    Messages:
    9
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #6
    You're right, thank you for that. I will change that now. The main problem is that the probabilities just don't change, it's easy to steal any car!
     
    LongshotMP, Apr 26, 2017 IP
  7. #7
    Do your math, two thirds your cars are all 10%.

    >90% else >80% is 10% of your random since it's only doing 81..90 as the match. If it's not >80 but is >70, that's 10%.... SO, given how your original post's code is worded:

    Assuming a random 1..100 (I'd be doing 0..99 personall)

    
    #     Range      %
    1      100       1
    2   FAILS ELSE   0
    3     91..99     9
    4     81..90    10
    5     71..80    10
    6     61..70    10
    7     51..60    10
    8     41..50    10
    9     36..40     5
    10    26..35    10
    11    21..25     5
    12     6..20    15
    ELSE {
    ?!?    1..5      5
    
    Code (markup):
    Six of your cars have a 10% probability, one car has 9%, one car has 15%, two cars have 5%, one car has 1%, and you've got a 5% chance of a swing and a miss since there's an else you didn't show us the result of.
     
    deathshadow, Apr 26, 2017 IP
  8. LongshotMP

    LongshotMP Peon

    Messages:
    9
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #8
    You've actually fixed my issue. Thank you so much!
     
    LongshotMP, Apr 26, 2017 IP