Scientific Computing with Python Projects - Probability Calculator

Tell us what’s happening:
What’s happening is that I don’t understand why the program doesn’t pass the final test when I use random.shuffle() instead of random.randint() in the draw() method. Could someone please explain why it doesn’t work? Because that would be a more efficient way of doing it, I believe… the list wouldn’t have to be reassembled every time we pop() from the middle…
P.s. sorry the code is without indents, idk why but I can’t seem to make indents in these comments… :confused:

Your code so far
Here’s my Replit, just check the draw() method…

Or here:

def draw(self, amt):
if amt>len(self.contents): return self.contents
return [self.contents.pop() for _ in range(amt)]

It draws just fine, but doesn’t pass the test with the following failure:

self.assertAlmostEqual(actual, expected, delta = 0.01, msg = ‘Expected experiment method to return a different probability.’)
AssertionError: 0.261 != 0.272 within 0.01 delta (0.01100000000000001 difference) : Expected experiment method to return a different probability.

Then I have this version of draw that passes the test:

def draw(self, amt):
if amt>len(self.contents): return self.contents
return [self.contents.pop(random.randint(0, len(self.contents)-1)) for _ in range(amt)]

I just want to know why it doesn’t work with random.shuffle(), when, in theory, I believe it should do the exact same thing…

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 12) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Mobile Safari/537.36

Challenge: Scientific Computing with Python Projects - Probability Calculator

Link to the challenge:

Maybe it is not the method itself, but the number of shuffles?

Here you shuffle one time and then take the items that are at the end

Here you pick in each turn randomly one remaining item

Does it works when running random.shuffle between each pop ?

Or maybe the test uses the result that you get when you don’t change the order of the list, but choose one remaining element randomly, and the fixed seed for the probability causes the fail

No, it doesn’t. Actually, it becomes even worse: AssertionError: 0.252 != 0.272 within 0.01 delta (0.020000000000000018 difference)

Besides, that defeats the purpose. The whole point is that when you shuffle the list and then pop off the last element - you essentially end up popping off a random element (since the list is now in random order). And the new list (consisting of randomly popped off elements) is returned. The list of remaining elements is no longer used in the exercise, so it no longer matters.

Why does the order of the list matter? Popping off the end of a randomly ordered list should be the same as randomly popping off a fixed order list… No?

What fixed seed? It gets shuffled to a random order for each new run, so… it’s not fixed…

The draw() method simply return a list of n random elements from the original… if you run the draw method on its own - you’ll see that it works… so why doesn’t it pass?

I really don’t get it… :cry:

in is the line prob_calculator.random.seed(95)
So it’s not really random
I inserted a print statement with the drawn balls into my project, and it always chooses exactly the same colors: first experiment: red green blue blue
second experiment blue green green green

If the use of shuffle uses an unexpected different way, that might mess the result up

Oooh, that actually makes a lot of sense… so maybe they really are both random (pseudo random), but they’ll end up giving different results with a fixed seed due to different implementation, correct? Which means that for practical purposes (when actually looking for random results), you can safely use my method with shuffle, right?

Yes, I think so. After all you don’t want the test fail just because you randomly got that once in a century event to draw the same balls in every experiment … :slightly_smiling_face:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.