Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在i和j之间生成n个随机数_Python_R_Random - Fatal编程技术网

Python 在i和j之间生成n个随机数

Python 在i和j之间生成n个随机数,python,r,random,Python,R,Random,我想用n个介于I和j之间的数字创建一个随机数。例如,对于n=10、i=1和j=5,需要这样的输出:2414243211。我在R中使用了以下代码: paste(floor(runif(10,1,5)),collapse="") #runif create 10 random number between 1 and 5 and floor make them as integer and finally paste makes them as a sequence of numbers inste

我想用n个介于I和j之间的数字创建一个随机数。例如,对于n=10、i=1和j=5,需要这样的输出:
2414243211
。我在R中使用了以下代码:

paste(floor(runif(10,1,5)),collapse="") #runif create 10 random number between 1 and 5 and floor make them as integer and finally paste makes them as a sequence of numbers instead of array. 
我想用Python做同样的事情。我发现
random.uniform
,但它生成1个数字,我不想使用循环

import random
import math
math.floor(random.uniform(1,5)) #just generate 1 number between 1 and 5
更新

  • i和j是介于0和9之间的整数,而n可以是任何整数
  • i和j决定可以在字符串中使用哪个数字,而n表示数字字符串的长度

如果你认为列表理解是循环的(实际上它们在很多方面都是),你就不会满意,但我会试试运气:

from random import randint

res = ''.join([str(randint(1, 5)) for _ in range(10)])
print(res)  #-> 4353344154
注:

  • 结果是一个字符串!如果需要整数,请强制转换为
    int
  • randint
    兼收并蓄;可能会生成并返回
    开始(
    1
    )和
    结束(
    5
    )。如果不需要,请修改它们(
    start=2
    end=4
  • 您使用的是
    random.uniform
    (以及随后使用的
    math.floor()
    )而不是简单的
    randint
    ,有什么原因吗

  • 如果我理解你的问题(我不确定我是否理解),并且你有Python 3.6,那么你可以使用
    random.choices

    >>> from random import choices
    >>> int(''.join(map(str, choices(range(1, 5), k=10))))
    2121233233
    
    该函数执行您想要的操作:

    >>> from random import choices
    >>> n, i, j = 10, 1, 5
    >>> population = list(map(str, range(i, j+1)))
    >>> ''.join(choices(population, k=n))
    '5143113531'
    

    在本机Python中,如果您熟悉R,则必须使用循环,然后您可能需要查看numpy/pandas。那么您想要一个带有这些数字的数字吗<代码>整数(''.join([str(math.floor(random.uniform(1,5)))表示范围内(10)])
    我会选择:
    '.join(map(str,choices(range(I,j+1),k=n))
    -无需将范围列为一个列表,只需让它从范围中采样,并将其映射即可。也许值得一提的是,
    choices
    是3.6+——在3.5上仍然有很多人……这和我在@JonClements上做的差不多,但是Raymond有没有理由在使用选项采样之前进行字符串转换*?想想
    范围(100000000)的情况
    可能是候选人才库,
    k=10
    是所需的全部。。。至少你只有一个10个元素的列表,而不是这个范围内的临时元素…很难从这个问题的措辞中判断哪一个是最好的。我的看法是,i和j是数字范围,所以会有10个或更少。同时,看起来n可能会变大。@RaymondHettinger是的,你是对的。i和j在此范围内[0,9],n可以是任何整数。谢谢。因为我想创建数千个不同长度的数字,所以我更喜欢使用循环来实现这一目的,而不是生成随机数。所以,没有循环就无法生成超过1个数?对于3,不,我只是不知道
    randint
    。多谢各位much@Hadij有。只需将现有列表包装到另一个列表中
    res=['''.join([str(randint(1,5))表示范围内的(10)])表示范围内的(5)]
    >>> from random import choices
    >>> n, i, j = 10, 1, 5
    >>> population = list(map(str, range(i, j+1)))
    >>> ''.join(choices(population, k=n))
    '5143113531'