Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 具有多个set.seed()的唯一变量_Python_Python 3.x_Random - Fatal编程技术网

Python 具有多个set.seed()的唯一变量

Python 具有多个set.seed()的唯一变量,python,python-3.x,random,Python,Python 3.x,Random,我想生成一个193次的随机数,但是设置一个种子,这样以后我就可以为每个实例得到相同的数。因为找不到设置种子X次的函数,所以我编写了以下代码: rand_list_raw = [] val_length = int(len(all_articles)*0.15) seeds = list(range(0,val_length)) index = 0 while len(rand_list_raw) < val_length: seed = seeds[index]

我想生成一个193次的随机数,但是设置一个种子,这样以后我就可以为每个实例得到相同的数。因为找不到设置种子X次的函数,所以我编写了以下代码:

rand_list_raw = []
val_length = int(len(all_articles)*0.15)
seeds = list(range(0,val_length))
index = 0

while len(rand_list_raw) < val_length:  
        seed = seeds[index]
        random.seed(seed)
        rand_num = random.randrange(0, 1290, 1)
        rand_list_raw.append(rand_num)
        index += 1
有没有办法保证不同的数字(除了硬编码)


另外,我知道我也可以创建一个在范围内具有唯一随机数的列表并将其导出。但这不是重点

为什么要播种n次?你可以从一个开始的种子开始一步一步地进行,并且总是有相同的顺序。考虑:

rand_dict = {}
rand_list = []
val_length = int(len(all_articles)*0.15)
seed = 1 #whatever you want
step = 0
random.seed(seed)
while len(rand_list_raw) < val_length: 
    step+=1
    rand_num = random.randrange(0, 1290, 1)
    try:
        test = rand_dict[rand_num] #check if key exists if it does we dont do anything
    except KeyError:
        rand_list.append(rand_num)
        rand_dict[rand_num] = step
rand_dict={}
随机列表=[]
val_长度=整数(长度(所有物品)*0.15)
种子=1#你想要什么都行
步长=0
随机。种子(种子)
而len(rand_list_raw)
如果你想再次得到同样的数字。只需使用相同的种子再次播种,并使用生成器对dict中存储的相同数量的步骤进行迭代

示例以获取您的号码。使用seed=1,在步骤=5时,值为241

random.seed(seed)
step = rand_dict[241]
i = 0
while(i<step)
    buf = random.randrange(0,1290,1)
    i+=1
print(buf)
>> 241
random.seed(种子)
步骤=随机数字[241]
i=0
而(i>241)

除了“这不是重点”之外,您的“Ps”中的解决方案还有什么问题?
random.seed(seed)
step = rand_dict[241]
i = 0
while(i<step)
    buf = random.randrange(0,1290,1)
    i+=1
print(buf)
>> 241