Performance difference between numpy.random and random.random in Python

Paolo6

I want to see what random number generator package is faster in my neural network.

I am currently changing a code from github, in which both numpy.random and random packages are used to generate random integers, random choices, random samples etc.

The reason that I am changing this code is that for research purposes I would like to set a global seed to be able to compare accuracy performance for different settings of hyperparameters. The problem is that at this moment I have to set 2 global seeds, both for the random package and for the numpy package. Ideally, I would like to set only one seed as drawings from two sequences of random number generators might become correlated more quickly.

However, I do not know what package will perform better (in terms of speed): numpy or random. So I would like to find seeds for both packages that correspond to exactly the same Mersenne Twister sequence. In that way, the drawings for both models are the same and therefore also the number of iterations in each gradient descent step are the same, leading to a difference in speed only caused by the package I use.

I could not find any documentation on pairs of seeds that end up in the same random number sequence for both packages and also trying out all kind of combinations seems a bit cumbersome.

I have tried the following:

np.random.seed(1)
numpy_1=np.random.randint(0,101)
numpy_2=np.random.randint(0,101)
numpy_3=np.random.randint(0,101)
numpy_4=np.random.randint(0,101)
for i in range(20000000):
    random.seed(i)
    random_1=random.randint(0,101)
    if random_1==numpy_1:
        random_2=random.randint(0,101)
        if random_2==numpy_2:
            random_3=random.randint(0,101)
            if random_3==numpy_3:
                random_4=random.randint(0,101)
                if random_4==numpy_4:
                    break
print(np.random.randint(0,101))
print(random.randint(0,101))

But this did not really work, as could be expected.

Mstaino

numpy.random and python random work in different ways, although, as you say, they use the same algorithm.

In terms of seed: You can use the set_state and get_state functions from numpy.random (in python random called getstate and setstate) and pass the state from one to another. The structure is slightly different (in python the pos integer is attached to the last element in the state tuple). See the docs for numpy.random.get_state() and random.getstate():

import random
import numpy as np
random.seed(10)
s1 = list(np.random.get_state())
s2 = list(random.getstate())

s1[1] = np.array(s2[1][:-1]).astype('int32')
s1[2] = s2[1][-1]

np.random.set_state(tuple(s1))

print(np.random.random())
print(random.random())
>> 0.5714025946899135
0.5714025946899135

In terms of efficiency: it depends on what you want to do, but numpy is usually better because you can create arrays of elements without the need of a loop:

%timeit np.random.random(10000)
142 µs ± 391 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

%timeit [random.random() for i in range(10000)]
1.48 ms ± 2.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In terms of "randomness", numpy is (according to their docs), also better:

Notes: The Python stdlib module "random" also contains a Mersenne Twister pseudo-random number generator with a number of methods that are similar to the ones available in RandomState. RandomState, besides being NumPy-aware, has the advantage that it provides a much larger number of probability distributions to choose from.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

python 2 vs python 3 performance of random, particularly `random.sample` and `random.shuffle`

Differences between numpy.random.rand vs numpy.random.randn in Python

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

Difference between various numpy random functions

Difference between np.random.seed() and np.random.RandomState()

Difference between np.random.seed() and np.random.RandomState()

Differences between numpy.random and random.random in Python

Differences between numpy.random and random.random in Python

What is the difference between random randint and randrange?

Random drop in performance

Distribution difference between Kotlin Random and Java Random

What's the difference between numpy.random vs numpy.random.Generate

Does the difference between random seeds matter?

Is there a difference between the input paramaters of numpy.random.choice and random.choice?

Whats the difference between os.urandom() and random?

Difference between Importance(random forest) and RandomForest$importance

what is the difference between $(($RANDOM % 10 +1)) and $(( ( RANDOM % 10 ) + 1 ))

Random number generator performance varies between platforms

What is the difference between the random.choices() and random.sample() functions?

What is the difference between numpy.random's Generator class and np.random methods?

Get floating random numbers between -2 and 2 only using random.random in numpy?

Difference between RANDOM and SRANDOM in Bash

What is the difference between Pycrypto's Random.get_random_bytes and a simple random byte generator?

Is there any difference between random in C and random in Java?

What is the difference between Get-Random and random

Difference between two methods of random point generation

Difference between following examples (random number)

Difference Java Random and Kotlin Random

Quicksort performance in Python - random pivot vs static