我希望以下函数为数据帧中的每一行返回一个不同的数字,但每次该函数运行时都返回相同的数字。
谢谢。
def inc14(p):
if p==1:
return random.randint(1,2000)
elif p==2:
return random.randint(2001,3000)
elif p==3:
return random.randint(3001,4000)
elif p==4:
return random.randint(4001,5000)
elif p==5:
return random.randint(5001,7000)
elif p==6:
return random.randint(7001,9000)
elif p==7:
return random.randint(9001,12000)
elif p==8:
return random.randint(12001,15000)
elif p==9:
return random.randint(15001,20000)
elif p==10:
return random.randint(20001,40000)
elif p==11:
return 0.01
else:
return np.NaN
data['inc_cont14']=data['inc14'].apply(inc14)
答案 0 :(得分:1)
随机性只是随机的,因为种子每次都会变化。如果您设置种子,则由于您从同一种子开始撒酸,每次都会得到相同的结果。
import random
def inc14(p):
random.seed(10)
if p == 1:
return random.randint(1, 2000)
elif p == 2:
return random.randint(2001, 3000)
elif p == 3:
return random.randint(3001, 4000)
elif p == 4:
return random.randint(4001, 5000)
elif p == 5:
return random.randint(5001, 7000)
elif p == 6:
return random.randint(7001, 9000)
elif p == 7:
return random.randint(9001, 12000)
elif p == 8:
return random.randint(12001, 15000)
elif p == 9:
return random.randint(15001, 20000)
elif p == 10:
return random.randint(20001, 40000)
elif p == 11:
return 0.01
else:
return None
return np.NaN
for _ in range(10):
print(inc14(4), inc14(7))
输出
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
答案 1 :(得分:1)
定义的范围无关紧要:
如果定义的范围无关紧要,请参见下面的示例:
import random
import pandas as pd
random.seed(42) # Seed is here to always produce the same numbers
data = {'Name':['Tom', 'nick', 'krish', 'jack'], 'Age':[20, 21, 19, 18]}
df = pd.DataFrame(data) #create a dummy dataframe
# The dataframe has 4 rows. So we need 4 random numbers.
# If we want to generate 4 random numbers, without duplicates we can use random.sample
# In this example we sample 4 random number in the range of 0-399
range_multiplier = 100
df['Random'] = random.sample(range(len(df.index)*range_multiplier), len(df.index))
print(df)
输出:
Name Age Random
0 Tom 20 327
1 nick 21 57
2 krish 19 12
3 jack 18 379
如果您使用的种子与我使用的种子相同,则可以运行相同的代码,并且将获得与我相同的随机数。
定义的范围很重要:
如果您在此需要此范围,则该新功能会短很多,但是您必须准备所有数字。
random.seed(42) # Seed is here to always produce the same numbers
# for all p(1-10) and their ranges (1-2000, 2001-3000, 3001-4000,...)
# we generate a dictionary with p as the key
# and as value a list of all numbers in the defined range
# without duplicates with random.sample
p_numbers = {
1: random.sample(range(1, 2001), 2000),
2: random.sample(range(2001, 3001), 1000),
...
10: random.sample(range(20001,40001), 20000)
}
def inc14(p,p_numbers):
if p >= 1 and p<=10:
# take the first element of the number and remove it
# from the list (to avoid taking it again)
return p_numbers[p].pop(0)
elif p == 11:
return 0.01
else:
return np.nan
data['inc_cont14']=data['inc14'].apply(inc14,p_numbers)
我们再次需要种子来避免重复。
我们用其p的可用数字创建一个字典。如果p在1到10之间,我们从字典中取出数字,然后从那里删除,以免两次。