利用python代码实现:
Way1:首先生成2个有3个数字的数组,再对这2个数组中数据进行变异,变异次数为10次,输出初始数据及变异后的数据
import random
arr1 = [random.randint(1, 10) for _ in range(3)]
arr2 = [random.randint(1, 10) for _ in range(3)]
print("初始数据为:", arr1, arr2)
print('---------------------------------')
n = 10
for i in range(n):
new_arr1 = [random.randint(1, 10) for _ in range(3)]
new_arr2 = [random.randint(1, 10) for _ in range(3)]
print("变异后数据:", new_arr1, new_arr2)
输出结果类似于:
Way2:下面将数组 a = [10, 2, 5] 进行变异 10 次
import random
a = [10, 2, 5]
n = 10
results = []
for i in range(n):
index = random.randint(0, len(a) - 1)
new_value = random.randint(1, 100)
a[index] = new_value
results.append(a[:])
for i, result in enumerate(results):
print(f"变异数据 {i+1}: {result}")
输出结果类似于: