0

I am in the middle of creating a game, and users can steal cars. However, when it comes to clicking "steal car", I need to make the better cars more difficult to steal (car 1 being the most difficult, car 2 being the next difficult...etc).

I have a set of random probability at the moment within my PHP script, although it doesn't seem to work. It feels as though each car is as equally difficult to steal. As a side note, you will see that each car produces it's own picture once stolen. Can someone help? Thanks!

if($query1>="3"){


$random_car = rand (0,99);


if (($random_car)>98)
{$car = $car_1; $pic = $car1_pic;}
else
{
if (($random_car)>97)
{$car = $car_2; $pic = $car2_pic;}
else
{
if (($random_car)>94)
{$car = $car_3; $pic = $car3_pic;}
else
{
if (($random_car)>86)
{$car = $car_4; $pic = $car4_pic;}
else
{
if (($random_car)>77)
{$car = $car_5; $pic = $car5_pic;}
else
{
if (($random_car)>72)
{$car = $car_6; $pic = $car6_pic;}
else
{
if (($random_car)>65)
{$car = $car_7; $pic = $car7_pic;}
else
{
if (($random_car)>58)
{$car = $car_8; $pic = $car8_pic;}
else
{
if (($random_car)>48)
{$car = $car_9; $pic = $car9_pic;}
else
{
if (($random_car)>35)
{$car = $car_10; $pic = $car10_pic;}
else
{
if (($random_car)>19)
{$car = $car_11; $pic = $car11_pic;}
else
{
if (($random_car)>1)
{$car = $car_12; $pic = $car12_pic;}
else
{

$car = $car_13; $pic = $car13_pic;

}//car 12 else
}//car 11 else
}//car 10 else
}//car 9 else
}//car 8 else
}//car 7 else
}//car 6 else
}//car 5 else
}//car 4 else
}//car 3 else
}//car 2 else
}//car 1 else

3 Answers3

0

The reason you feel the randomization isn't working too well is that you aren't creating big enough margins between the options.

For car 1 it will only get selected if $random_car is 99 or 100. Car 2 will only get selected if $random_car is 97 or 98 and that's it. If it's 99 or 100 then car 1 will be selected, so both car 1 and car 2 have the same probability of being chosen. There are higher chances of the cars at the bottom of the list being selected, but the margins aren't that much higher.

Car 1 has a 1 in 100 chance of being selected, where car 12 has an 18 in 100 chance of being selected. This is the biggest margin I saw.

As far as your code is concerned, those nested if statements are a nightmare. I'd rewrite them using the following syntax:

if() {
  // do something
}
elseif() {
  // do something else
}
else {
  // do if nothing else worked
}
aylnon
  • 371
  • 2
  • 8
0

Your if statement look a bit wired. I think you can write them much easier instead of boxing up such a big if structure. Here is a bit shorter version where you can define an array with your values.

$carsSpecs = [
    '1' => [
        'min' => 97,
        'max' => 98,
        'car' => 123,
        'carpic' => 'test.jpeg'
    ],
];

$random_car = rand(0,99);
$car = '';
$pic = '';

foreach($carsSpecs as $car) {
    if($random_car > $car['min'] && $random_car < $car['max']) {
        $car = $car['car'];
        $pic = $car['carpic'];
    }
}

Now you can define every car in the array and work with them much easier. Perhaps you have an existing array and you can use them directly.

There are some other styles to work with the array but then it's much more complicated.

The last thing is that you should make wider ranges between the numbers.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
0

I suggest implementing all that logic as data.

For example:

Generates a random vehicle

<?php

  $cars = [
    ["Buick", "buick.jpg" ],
    ["Mazda", "mazda.jpg" ],
    ["Honda", "honda.jpg" ],
    ["Acura", "acura.jpg" ],
    ["Dodge", "dodge.jpg" ],
    ["Tesla", "tesla.jpg" ],
    ["Volvo", "volvo.jpg" ],
    ["Ford",  "ford.jpg"  ],
    ["Kia",   "kia.jpg"   ],
    ["Audi",  "audi.jpg"  ],
    ["BMW",   "bmw.jpg"   ],
    ["Camry", "camry.jpg" ],
    ["Lexus", "camry.jpg" ],  # Lexus is just a Camry with a leather interior
  ];

  $frequency_table = [ 1, 19, 35, 48, 58, 65, 72, 77, 86, 94, 97, 99, 101 ];

  $r = rand(0,99);

  for ($ix = 0; $frequency_table[$ix] < $r; ++$ix );

  list($name, $img) = $cars[$ix];

  echo "You stole a $name <img src=/images/$img>\n";
?>

Output

You stole a Dodge <img src=/images/dodge.jpg>

...

You stole a Ford <img src=/images/ford.jpg>
Jim U
  • 3,318
  • 1
  • 14
  • 24