如何从给定范围内统一得到n个唯一的数字?

问题描述 投票:0回答:1

我有一个整数范围 [0, Z)。我需要获得 n (其中 n <= Z) random numbers from this range, but they have to be unique. So I know I can just code up rejection sampling to do this, but I'm wondering if there's a one line python function that can do this for me?

python numpy range uniform-distribution
1个回答
4
投票

为什么不使用无放回随机抽样

import numpy as np

n = 25
a = range(26)
out = np.random.choice(a, size=n, replace=False)
len(np.unique(out))
>>>25
© www.soinside.com 2019 - 2024. All rights reserved.