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,
    SEMrush
    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 at 5:07 PM
    LongshotMP, Apr 18, 2017 IP
    SEMrush
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,398
    Likes Received:
    679
    Best Answers:
    145
    Trophy Points:
    420
    #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. #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 at 4:44 PM
    JEET, Apr 20, 2017 at 4:22 PM IP
    matt_62, ThePHPMaster and sarahk like this.
  4. LongshotMP

    LongshotMP Peon

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    1
    #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 at 6:52 AM IP
    JEET likes this.