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'?
normalvariate() normalvariate() is an inbuilt method of the random module. It is used to return a random floating point number with normal distribution.
import random imports the random module, which contains a variety of things to do with random number generation. Among these is the random() function, which generates random numbers between 0 and 1. Doing the import this way this requires you to use the syntax random. random() .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With