简体   繁体   English

生成N个随机整数的数组,介于1和K之间,但每个数字至少包含一个

[英]Generate an array of N random integers, between 1 and K, but containing at least one of each number

I need to generate a matrix of N random integers between 1 and K , where each number appears at least once, having K ≤ N . 我需要生成一个介于1K之间的N随机整数的矩阵,其中每个数字至少出现一次, K ≤ N

I have no problem using a call to numpy.random.random_integers() and checking the number of distinct elements, when K is much less than N , but it's harder to get a valid array when K approximates to N . K远小于N ,我可以使用对numpy.random.random_integers()的调用并检查不同元素的数量,但是当K接近N时,很难获得有效的数组。

Is there any nice way to get this done? 有什么好办法可以做到这一点吗? Any hint would be appreciated. 任何提示将不胜感激。

I don't think there's any built in functionality, but you could always make the exact array you want and shuffle/reshape. 我认为没有任何内置功能,但是您始终可以制作出想要的确切数组并进行随机播放/重塑。

Something like, 就像是,

raw_array = np.append(np.arange(1, K+1), np.random.randint(1, K+1, size=(N-K)))
np.random.shuffle(raw_array)

This guarantees you have 1 to K from your arange, then the remaining required numbers (N - K) are filled by a random number from 1 to K. 这样可以保证您的范围从1到K,然后剩余的所需数字(N-K)由1到K的随机数填充。

Edited to specifically get 1 through K, rather than 0 to K - 1. 编辑为专门通过K获得1,而不是0至K-1。

Fill the matrix iteratively with numbers 1-K so if K was 2, and N was 4, [1,2,1,2]. 用数字1-K迭代填充矩阵,所以如果K为2,N为4,则[1,2,1,2]。 Then randomly generate 2 random numbers between 1-N where the numbers don't equal, and swap the numbers at those positions. 然后在数字不相等的1-N之间随机生成2个随机数,并在这些位置交换数字。

使用xrange(k)填充K个数字,然后使用随机数生成器填充(nk)个数字

您正在寻找np.random.choice(N, size=K, replace=False) doc ,它将返回K不同的数字,最多N

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:生成两个相差至少k的随机整数 - Python: Generate two random integers that differ by at least k 生成随机数组,每个整数具有多个外观 - Generate random array of integers with a number of appearance of each integer 从给定的元素列表生成随机的numpy数组,每个元素至少重复一次 - Generate random numpy array from a given list of elements with at least one repetition of each element 生成 0 到 9 之间的随机整数 - Generate random integers between 0 and 9 给定一个由 N 个整数组成的数组和一个整数 K,找出该数组中总和等于 K 的元素对的数量 - Given an array of N integers, and an integer K, find the number of pairs of elements in the array whose sum is equal to K 如何生成随机数,每个随机数至少与所有其他元素相差x? - How to generate random numbers with each random number having a difference of at least x with all other elements? 从嵌套在列表中的每个列表中选择一个随机项,它们之间有所不同,然后生成一个列表n次(重采样) - Select one random item from each list nested in a list, different between them, and generate a list n times (RESAMPLING) 使用来自不同范围的随机数生成 numpy 数组的每一列 - Generate each column of the numpy array with random number from different range 一个 liner 生成一个 0 到 100 之间的随机数,但最后一位数字以 0、1、2、3、4 结尾 - one liner to generate a random number between 0 and 100 but last digit ends in 0, 1, 2, 3, 4 从多个列表中选择随机数,它必须至少包含每个列表中的一个数字 - Choose random numbers from multiple lists and it must contain at least one number from each list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM