bbartling
bbartling

Reputation: 3502

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. 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. Would some sort of function be the best method to make this work? Thanks

Upvotes: 1

Views: 1494

Answers (2)

Sandeep Mutkule
Sandeep Mutkule

Reputation: 11

use this,

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

Upvotes: 1

Matthew
Matthew

Reputation: 193

I would recommend you use the random module and use random.randint which generates a random integer

import random 

number = random.randint(0,2)

Upvotes: 2

Related Questions