Going fast! #DataquestChallenge Premium Annual Offer:
500 get 50% & the next 1000 get 40% off.

"Unpredictable" results are actually predictable


This problem asks us use parallel processing to add all values between 1 and 10,000 exclusive.
The lesson states the code used is actually unpredictable, with one instance returning 38769868 instead of the answer, 49995000.

Upon running the correct answer:

def sum_values(first, last, shared_value):
    for i in range(first, last):
        shared_value.value += i

def sum_with_two_processes():
    N = 10000

    shared_value = multiprocessing.Value("i")
    process1 = multiprocessing.Process(target=sum_values, args=(1, N // 2, shared_value))
    process2 = multiprocessing.Process(target=sum_values, args=(N // 2, N, shared_value))


    return shared_value.value

# Add code below
results = []
for _ in range(10):
    result = sum_with_two_processes()


I get this output:

[49995000, 49995000, 49995000, 49995000, 49995000, 49995000, 49995000, 49995000, 49995000, 49995000]

Which is the correct answer for all 10 iterations.
I thought this method was unpredictable?

The stars aligned on this one. It’s a “even a wrong clock is right twice a day” kind of situation, the randomness sometimes gets it right.

If you ran it multiple times and got that result multiple times, that’s probably because of a cached answer. I don’t expect it to happen again if you try it again.

1 Like

I see. Thanks for your response :+1: