1

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

5
  • Do you need to use numpy? There are a few methods in random that would do what you're looking for. Commented Dec 10, 2018 at 18:50
  • Why not get a one-element array and select its 0th element? Commented Dec 10, 2018 at 18:51
  • why will it return an array? it is perfectly capable of returning an int. Commented Dec 10, 2018 at 18:54
  • 1
    "numpy.random.randint seems like it would work but it will return an array" - it'll only do that if you tell it to. (Also, if you're using NumPy and you don't want an array, you may be using NumPy wrong.) Commented Dec 10, 2018 at 18:54
  • action = random.randint(0,2) totally did the trick thank you for all the fast responses! Commented Dec 10, 2018 at 18:55

2 Answers 2

2

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

import random number = random.randint(0,2) 
Sign up to request clarification or add additional context in comments.

Comments

1

use this,

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.