简体   繁体   English

Numpy生成随机整数

[英]Numpy generate random whole number

Is there a way to generate a whole number/integer?有没有办法生成整数/整数? I am attempting to find a way to generate a random number that is either a 0 or 1 or 2.... and Nothing in between.我试图找到一种方法来生成一个随机数,它要么是 0 要么是 1 要么是 2 .... 中间没有任何东西。 If I use:如果我使用:

import numpy as np

for i in range(5):
    action = np.random.uniform(0,2,1)#take random action between 0 1 or 2
    print(action)

Returns:返回:

[0.18429578]
[1.19763353]
[1.93240698]
[1.44706511]
[0.31182739]

numpy.random.randint seems like it would work but it will return an array. numpy.random.randint似乎可以工作,但它会返回一个数组。 Would some sort of function be the best method to make this work?某种功能是使这项工作的最佳方法吗? Thanks谢谢

I would recommend you use the random module and use random.randint which generates a random integer 我建议您使用random模块并使用random.randint生成一个随机整数

import random 

number = random.randint(0,2)

use this,用这个,

for i in range(5):
    action = np.random.randint(0,3)
    print(action)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM