Python 随机数-每次运行时的相同数-每行的不同数

Python 随机数-每次运行时的相同数-每行的不同数,python,pandas,Python,Pandas,我希望下面的函数会为数据帧中的每一行返回不同的数字,但每次函数运行时返回相同的数字 谢谢 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

我希望下面的函数会为数据帧中的每一行返回不同的数字,但每次函数运行时返回相同的数字

谢谢

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)

“随机”仅在种子每次更改时才是随机的。如果你设定种子,你每次都会得到相同的结果,因为你是从同一个种子开始的

随机导入
def inc14(p):
随机。种子(10)
如果p==1:
返回random.randint(1,2000)
elif p==2:
返回random.randint(2001,3000)
elif p==3:
返回random.randint(30014000)
elif p==4:
返回random.randint(40015000)
elif p==5:
返回random.randint(50017000)
elif p==6:
返回random.randint(70019000)
elif p==7:
返回random.randint(900112000)
elif p==8:
返回random.randint(1200115000)
elif p==9:
返回random.randint(150012000)
elif p==10:
返回random.randint(200014000)
elif p==11:
返回0.01
其他:
一无所获
返回np.NaN
对于范围(10)内的uu:
印刷品(inc14(4),inc14(7))
输出

4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341

“随机”仅在种子每次更改时才是随机的。如果你设定种子,你每次都会得到相同的结果,因为你是从同一个种子开始的

随机导入
def inc14(p):
随机。种子(10)
如果p==1:
返回random.randint(1,2000)
elif p==2:
返回random.randint(2001,3000)
elif p==3:
返回random.randint(30014000)
elif p==4:
返回random.randint(40015000)
elif p==5:
返回random.randint(50017000)
elif p==6:
返回random.randint(70019000)
elif p==7:
返回random.randint(900112000)
elif p==8:
返回random.randint(1200115000)
elif p==9:
返回random.randint(150012000)
elif p==10:
返回random.randint(200014000)
elif p==11:
返回0.01
其他:
一无所获
返回np.NaN
对于范围(10)内的uu:
印刷品(inc14(4),inc14(7))
输出

4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341
4586 11341

定义的范围无关紧要:

这里是一个运行示例,如果定义的范围不重要,如果它们重要,请参见以下内容:

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)
random.seed(42)#seed总是产生相同的数字
#适用于所有p(1-10)及其范围(1-2000、2001-3000、3001-4000等)
#我们生成一个以p为键的字典
#以及定义范围内所有数字的列表作为值
#没有随机样本的重复项
p_编号={
1:随机抽样(范围(2001年1月),2000年),
2:随机样本(范围(2001,3001),1000),
...
10:随机抽样(范围(20000140001),20000)
}
def inc14(p,p_编号):

如果p>=1和p定义的范围无关紧要:

这里是一个运行示例,如果定义的范围不重要,如果它们重要,请参见以下内容:

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)
random.seed(42)#seed总是产生相同的数字
#适用于所有p(1-10)及其范围(1-2000、2001-3000、3001-4000等)
#我们生成一个以p为键的字典
#以及定义范围内所有数字的列表作为值
#没有随机样本的重复项
p_编号={
1:随机抽样(范围(2001年1月),2000年),
2:随机样本(范围(2001,3001),1000),
...
10:随机抽样(范围(20000140001),20000)
}
def inc14(p,p_编号):

如果p>=1,你的数据看起来像什么?你们有几排?这些间隔很重要,还是你只需要每次都相同的随机数?你怎么说每次调用函数时都是相同的数?如果你需要可重复的随机数,请查看随机数生成器。你的数据是什么样子的?你们有几排?这些间隔很重要,还是你只需要每次都相同的随机数?你怎么说每次调用函数时都是相同的数?如果你需要可重复的随机数,请查看随机数生成器。感谢种子中的42所指的内容?@yona你可以选择任何你想要的数字。我喜欢42这个数字,仅此而已。@yona如果它对你有帮助,请将帖子标记为已接受,以便以后有类似问题的人可以看到它的解决方法。谢谢种子中的42所指的内容。@yona你可以选择任何你想要的数字。我喜欢数字42,仅此而已。@yona,如果它有助于你,请将帖子标记为已接受,以便以后有类似问题的人可以看到它得到了解决,以及如何解决。