It seems that there was nothing that can be reset with Numpy shuffle, so I made it. Please let me know if there is a better way to write it in the first place.
Shuffle with resettable_shuffle and restore with reset_shuffle.
test.py
import numpy as np
#array is an ordinary one-dimensional list
#Destructive method
def resettable_shuffle(array, seed):
np.random.seed(seed)
np.random.shuffle(array)
#array is an ordinary one-dimensional list
#Non-destructive method
def reset_shuffle(array, seed):
seq = np.arange(len(array))
np.random.seed(seed)
np.random.shuffle(seq)
tmp = np.c_[seq.T, np.array(array).T]
tmp = np.ndarray.tolist(tmp)
tmp = sorted(tmp)
tmp = np.array(tmp)
return np.ndarray.tolist(tmp[:,1])
seed = 321654
a = ["a","b","c","d","e","f"]
resettable_shuffle(a, seed)
print(a)
a = reset_shuffle(a, seed)
print(a)
Output result
['a', 'b', 'c', 'd', 'e', 'f']
['c', 'f', 'd', 'e', 'a', 'b']
['a', 'b', 'c', 'd', 'e', 'f']
Follow us: golf: @redshoga
Recommended Posts