Jump to content

Why don't these percentages add up as usual? Coding wrong?

Aleksbgbg

PLEASE DON'T LEAVE BECAUSE THE POST LOOKS LONG, ITS NOT! :D

 

Untitled.pngHere is a snippet of some code I did in python.

 

Let me give context:

This program is to calculate some important random stuff received in a game, it's basically knowing what you will happen and when - a bit of cheating, but then again - they do give us the percentages chance.

 

'probability' is a random number from 1 to 100. This is to represent the %-age chance of receiving that item out of a random item box (I'll call it that).

 

Now, with those if statements, I'm checking the %-age band compared to the random number. If it is within range, then that item is given.

These are the real life percentages:

- 1% for "log disks"

- 3% for "repair credits"

- 4% for "nano hull"

- 12% for "xenomit"

- 13% for "gg part"

- 67% for ammo

 

These add up to 100%. However, in my code, the ranges leave the last item (ammo) with only a 63% chance - why - where is that 4% gone?? What am I calculating wrong? Please tell me how to do this xD

||| Drakon (Desktop Build) |||

|| CPU: 3800X || Cooler: Kraken X63 || Motherboard: B450 Aorus M || Memory: HyperX DDR4-3200MHz 16G ||

|| Storage: 512GB 970 Pro + 500GB 850 EVO + 250GB 850 EVO + 1TB HDD + 2TB HDD || Graphics Card: RX 5700 XT Red Devil || Case: Thermaltake Core V21 || PSU: XFX XTR 750W 80+Gold || 

 

Link to comment
Share on other sites

Link to post
Share on other sites

I think the range() function does not include the stopping value. So, you're not counting 5,10,36, or 100.

Link to comment
Share on other sites

Link to post
Share on other sites

Your ranges are wrong.

 

You have added an extra 1% to the first 4 ranges. For example you say Repair Credits should be 3% but then the range includes number 2, 3, 4 and 5 which would be 4%.

Link to comment
Share on other sites

Link to post
Share on other sites

You shouldn't use range like that, it's a huge waste.

You can check if a number is in a range by doing

if x == 1:
	pass
elif 2 <= x <= 5:
	pass
elif 6 <= x <= 10:
	pass
.
.
.

 

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Brenz said:

Your ranges are wrong.

 

You have added an extra 1% to the first 4 ranges. For example you say Repair Credits should be 3% but then the range includes number 2, 3, 4 and 5 which would be 4%.

Right that's it. Thanks! Was so simple, couldn't see it. Kept doing 5 - 2 = 3 :D

58 minutes ago, fizzlesticks said:

You shouldn't use range like that, it's a huge waste.

You can check if a number is in a range by doing


if x == 1:
	pass
elif 2 <= x <= 5:
	pass
elif 6 <= x <= 10:
	pass
.
.
.

 

Thanks! Didn't know about this! :)

||| Drakon (Desktop Build) |||

|| CPU: 3800X || Cooler: Kraken X63 || Motherboard: B450 Aorus M || Memory: HyperX DDR4-3200MHz 16G ||

|| Storage: 512GB 970 Pro + 500GB 850 EVO + 250GB 850 EVO + 1TB HDD + 2TB HDD || Graphics Card: RX 5700 XT Red Devil || Case: Thermaltake Core V21 || PSU: XFX XTR 750W 80+Gold || 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×