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:
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...)
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
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.
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.
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!
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.