What is the difference between random.normalvariate() and random.gauss() in python?

Question:

What is the difference between random.normalvariate() and random.gauss()?

They take the same parameters and return the same value, performing essentially the same function.

I understand from a previous answer that random.gauss() is not thread safe, but what does this mean in this context? Why should a programmer care about this? Alternatively posed, why was both a thread safe and non-thread safe version of included in Python’s ‘random’?

Asked By: richnis

||

Answers:

I’m not entirely sure about this but the Python Documentation says that random.gauss is slightly faster so if you’re OK with non-thread safe then you can go a little faster.

Answered By: yhussain

In a multi-threaded system, calling random.normalvariate twice very quickly in succession will cause the internal code of random.normalvariate to be run twice, potentially before the first call has had a chance to return. Internal variables may of the function may not be reset before the second, which may cause errors in the function output.

Successive calls to random.gauss must instead wait for earlier calls to return before being called themselves.

The advantage with random.normalvariate is therefore that it is faster, but may produce an erroneous output.

Answered By: richnis

Thread-safe pieces of code must account for possible race conditions during execution. This introduces overhead as a result of synchronization schemes like mutexes, semaphores, etc.

However, if you are writing non-reentrant code, no race conditions normally arise, which essentially means that you can write code that executes a bit faster. I guess this is why random.gauss() was introduced, since the python doc says it’s faster than the thread-safe version.

Answered By: VHarisop

This is an interesting question. In general, the best way to know the difference between two python implementations is to inspect the code yourself:

import inspect, random
str_gauss = inspect.getsource(random.gauss)
str_nv=inspect.getsource(random.normalvariate)

and then you print each of the strings to see how the sources differ. A quick look at the codes show that not only they behave differently multithread-wise, but also that the algorithms are not the same; for example, normalvariate uses something called the Kinderman and Monahan method, as per the following comments in str_nv:

# Uses Kinderman and Monahan method. Reference: Kinderman,
# A.J. and Monahan, J.F., "Computer generation of random
# variables using the ratio of uniform deviates", ACM Trans
# Math Software, 3, (1977), pp257-260.
Answered By: Lord Henry Wotton