想請問要如何讓output的ch能夠在FlipOnePosition()後仍保持一樣的值呢?而s則為flip後的值
output:
Before [1 0 1 1 1]
After [1 0 1 1 1]
s [1 0 1 1 0]
import random
import numpy as np
def FlipOnePosition(pk,num):
position = random.randint(0, num - 1)
if pk[position] == 0:
pk[position] = 1
else:
pk[position] = 0
return pk
if __name__ == "__main__":
ch = np.random.randint(0, 2, 5)
print("Before",ch)
s = FlipOnePosition(ch, 5)
print("After",ch)
print("s",s)
'''
output:
Before [1 0 1 1 1]
After [1 0 1 1 0]
s [1 0 1 1 0]
'''
import random
import numpy as np
def FlipOnePosition(pk,num):
ret = pk.copy()
position = random.randint(0, num - 1)
if ret[position] == 0:
ret[position] = 1
else:
ret[position] = 0
return ret
if __name__ == "__main__":
ch = np.random.randint(0, 2, 5)
print("Before",ch)
s = FlipOnePosition(ch, 5)
print("After",ch)
print("s",s)