简体   繁体   English

如何在列表中随机 1 到 5 之间的 10 个数字,并且列表必须包含所有数字

[英]how to random 10 numbers between 1 and 5 in list and the list must inculde all number

from random import randint
k=[]
for i in range(10):
    k.append(randint(1,5))
    k.sort()
print(k)

The output will be correct but sometimes it not include value from 1 to 5. for example, maybe k=[2,3,3,3,3,4,4,5,5,5] and not included 1. I need to include all numbers output 是正确的,但有时它不包括从 1 到 5 的值。例如,可能 k=[2,3,3,3,3,4,4,5,5,5] 并且不包括 1。我需要包括所有数字

10 random numbers between 1 and 5 means a list of [1,1,1,1,1,1,1,1,1,1] is a valid result - it is just very unlikely. 1 到 5 之间的 10 个随机数意味着[1,1,1,1,1,1,1,1,1,1]的列表是有效的结果 - 这是非常不可能的。

If you need to ensure 1 to 5 are in it, start with [1,2,3,4,5] and add 5 random numbers:如果您需要确保其中有 1 到 5,请从[1,2,3,4,5]开始并添加 5 个随机数:

from random import choices

k = [1,2,3,4,5]
k.extend( choices(range(1,6), k=5)) # add 5 more random numbers
k.sort()

print(k)

to get要得到

[1,1,2,2,3,4,4,5,5,5]  # etc.

The part choices(range(1,6), k=5) returns a list of 5 numbers in the range 1-5 without need for a loop.部分choices(range(1,6), k=5)返回范围为1-5的5个数字的列表,无需循环。

Using NumPy library could have priority over pythonic random to work with to do so;使用NumPy库可以优先于 pythonic random使用这样做; which may be very faster in huge data volumes :在大量数据中这可能会更快

import numpy as np

nums = np.arange(1, 6)                        # create integer numbers from 1 to (6 - 1) to ensure 1 to 5 will be contained
ran = np.random.random_integers(1, 6, 5)      # create 5 random integers from 1 to (6 - 1)
combine = np.concatenate((nums, ran))         # combining aforementioned two arrays together
np.random.shuffle(combine)                    # shuffling the created combined array

which will get arrays like [4 5 4 2 5 1 3 4 2 1] , which will contain all integers from 1 to 5. This array can be converted to list by combine.tolist() .这将得到 arrays 像[4 5 4 2 5 1 3 4 2 1] ,它将包含从 1 到 5 的所有整数。这个数组可以通过combine.tolist()转换为列表。

暂无
暂无

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

相关问题 Lambda function 用于将列表的每个元素与 1 到 10 之间的随机数相乘? - Lambda function for multiplying each element of list with random number between 1 and 10? 如何生成一个随机数,使其不是 2 的幂? 输出需要是 8 个随机数的列表 - How to generate a random number such that it is not a power of 2? The output needs to be a list of 8 random numbers 尝试创建 1-10 之间的 10 个随机数的列表,这些随机数在 python 中不重复 - Trying to create a list of 10 random numbers between 1-10 that do not repeat in python 从多个列表中选择随机数,它必须至少包含每个列表中的一个数字 - Choose random numbers from multiple lists and it must contain at least one number from each list 分布在-2和2之间且分布均匀的10个随机点的列表 - List of 10 random points between -2 and 2 with uniform distribution 创建随机数,排除列表中的数字 - Create random number excluding numbers in a list 随机数除了之前列在列表中的数字 - Random number except numbers written in list before 用于长数字列表的python随机数生成器 - python random number generator for long list of numbers 列出 4 个随机数 (0-9) 可以等于 10 的所有方式(使用任何运算符 +、-、*、/) - list all the ways 4 random numbers (0-9) can equal 10 (using any operator +,-,*,/) 将数字列表转换为以10为底的二进制数字 - Converting a list of numbers into a binary number base 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM