I don't want to affect only some random number seeds.

Asked 2 years ago, Updated 2 years ago, 28 views

For example, when func1 is called in the second line of test.py in the following two files, seed is fixed to 42, and b is also generated from seed42.I want to cause it without fixing the seed just now.
func.py

import numpy as np
def func1():
    np.random.seed (42)
    return np.random.rand()

test.py

import func
a=func.func1()
b=np.random.rand()

python

2022-09-30 21:37

2 Answers

Why don't you create a new generator using numpy.RandomState()?

b=np.random.RandomState().rand() 

--This answer is comment from metropolisUnity wiki This is what I posted as an answer.


2022-09-30 21:37

You can initialize with a different value by calling np.random.seed without arguments.

 from func import func1
import numpy as np


def func1():
    np.random.seed(42)#Static seed
    return np.random.rand()


def func2():
    np.random.seed()#Randomseed
    return np.random.rand()


def func3():
    np.random.seed(100)#Other static seed
    return np.random.rand()


defmain():
    val_1 = func1()
    val_2 = func2()
    val_3 = func3()
    print("val_1:{:.2}\nval_2:{:.2}\nval_3:{:.2}\n".format(val_1,val_2,val_3))


if__name__=="__main__":
    main()


2022-09-30 21:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.