0

I am generating random entries at selected positions in a matrix and I would like to see the same entries over different runs of the code. So I did the following but I'm not sure what I did wrong to get different entries on each run?

import numpy as np
import random
random.seed(10)

N = 5
G = [[0 for i in np.arange(N)] for j in np.arange(N)]
for i in np.arange(N):
    for j in np.arange(N):
        if i==j:
            G[i][j] = 0
        else:
            if abs(i-j) <= 2:
                random.seed(10)
                G[i][j] = round(np.random.uniform(0,1),2)
            else:
                G[i][j] = 0

print(G)
1
  • You set the seed for Python's random package, but you called NumPy's RNG instead. You didn't touch that seed. Commented Feb 13, 2020 at 22:15

1 Answer 1

2

You can replace

random.seed(10)

by

np.random.seed(10)

to have a fixed random state. Since you use NumPy's random module to generate the random number, you ought to use np.random.seed() to fix the random state. That is how I understood it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.