r/learnpython • u/Purple4cloud7 • 4d ago
I dont know whats different between these two functions
Im doing a course called building ai where were now working with probabilities. I thought it would be very simple but for some reason only the code that i find on github actually goes through the in-built authorizer.
The exercise wants me to ”write a program that prints ”i love” followed by one word: the additional word should be ’dogs’ with 80% probability, ’cats’ with 10% probability, and ’bats’ with 10% probability.”
Heres my code
import random
def main():
dogs = 0.80
cats = 0.90
bats = 1.00
if random.random() < dogs:
favourite = "dogs" # change this
elif random.random() < cats:
favourite = "cats" # change this
else:
favourite = "bats" # change this
print("I love " + favourite)
main()
The authorizer tells me im printing too many cats. Ive tried changing the comparisons to <= to make cats less probable but without any luck
Heres an example code i found on github through this link
https://github.com/Qianqianxia0910/Building-AI/blob/main/Exercise%204-%20Probabilities.py
import random
def main():
prob = 0.10
if random.random() < prob:
favourite = "bats"
elif random.random() >= (1-prob):
favourite = "cats"
else:
favourite = "dogs"
print("I love " + favourite)
main()
This code goes through flawlessly while in my eyes doing basically the same thing. Any explanation on what could be systematically different between these functions would be nice. Ive gone forward with the course but im just mighty curious on what might be different between these functions
6
u/Umustbecrazy 4d ago
Call random.random once, you get a new value each time the method is called.
randnum = random.random()
Compare randnum to your probabilities.
15
u/PeaAble4127 4d ago
You are generating a new number in each comparison. You need to reuse the result of the first random call, since you are defining your probabilities cumulatively.
6
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 4d ago
Personally I'd use
random.choicesfor this, as it supports weights.4
u/RagingGods 4d ago
Both are technically wrong, but yours will result in more skewed results. The github example is less skewed so it probably fell within the acceptable range of values the authorizer allows.
For yours, first if condition ensures "dogs" is printed about 80% of the time. That means the remaining 20% will move to the next elif. 90% of the remaining 20% will result in "cats", so about 18% of the original. That leaves the remaining "bats" to be at 2%. That's very far from the intended 80/10/10 split.
The example code ensures 10% will be "bats" and 10% of the remaining 90% to be "cats", and the remaining 81% of the original to be "dogs". Their split will be 81/9/10, which is much closer to 80/10/10.
The authorizer is probably coded to allow some margin of error (for randomness) so the 1% difference fell within the accepted range. Your solution, however, leaves about 8% difference.
3
u/pdpi 4d ago
The issue is that you’re calling random twice.
See it this way: flip a coin. Heads you like dogs, tails you like cats. The way you wrote it, heads you like dogs, tails you flip another coin that decides whether you like cats or bats.
2
u/jamesfowkes 4d ago
The other code you've posted doesn't work either, for the same reason others have said. It generates slightly too many dogs, slightly too few cats.
2
u/TheRNGuy 4d ago
Better call random once, or you won't have correct algorithm.
2
u/aiuniverse-3p3_here 4d ago
oh ypu are doing a course haha honestly bro both codes actually have the exact same bug the github one just got lucky with the math
basically when u call
random.random()twice python generates two totally different random numbers instead of checking the same one in your code firstifchecks if its< 0.80(80% dogs). when that fails which happens 20% of the time yourelifrolls a brand new number and checks if its< 0.90. so 90% of that remaining 20% becomes cats... which is0.20 * 0.90 = 18%cats and only 2% bats lol thats why the grader is complaining the github code does the same double roll thing but since they put the 10% checks first, the math works out to 10% bats 9% cats (0.90 * 0.10), and 81% dogs its super close to 80/10/10 so the automated test just lets it passjust save the random number in a variable at the top so it only rolls once:
python
1
u/zanfar 4d ago
The first function chooses "cats" 20% (not < 0.8) of 90% (< 0.9) of the time,
the second chooses "cats" 90% (not < 0.1) of 10% (> 0.9).
You don't have equivalent probabilities.
1
u/brasticstack 4d ago edited 4d ago
Just doing the math from the example:
p_bats = 0.10(technically 0.999... to the limit of float digits in your Python)The elif means we're combining probabilities, which is multiplicative:
p_cats = 0.1 * (1 - 0.1) = 0.09The probabilities must add up to 1, so
p_dogs = 1 - 0.1 - 0.09 = 0.81which is close enough for their checker.0
u/GnPDingo 4d ago
Two things i notice here. Your probabilities should total together to 1 And youre making a new random number on every check.
Make one random number by setting it to a variable then compare that number to all 3.
To test your code run a for loop 10 times. You should expect 8 dogs give or take 1 on every print
0
u/aiuniverse-3p3_here 4d ago
oh ypu are doing a course haha honestly bro both codes actually have the exact same bug the github one just got lucky with the math
basically when u call
random.random()twice python generates two totally different random numbers instead of checking the same one in your code firstifchecks if its< 0.80(80% dogs). when that fails which happens 20% of the time yourelifrolls a brand new number and checks if its< 0.90. so 90% of that remaining 20% becomes cats... which is0.20 * 0.90 = 18%cats and only 2% bats lol thats why the grader is complaining the github code does the same double roll thing but since they put the 10% checks first, the math works out to 10% bats 9% cats (0.90 * 0.10), and 81% dogs its super close to 80/10/10 so the automated test just lets it passjust save the random number in a variable at the top so it only rolls once:
python