2

I'm learning Numpy module from scratch going through the official Numpy documentation.
Now my goal is to create a random (3, 3) array.

>>> np.random.rand(3,3)

However the output I recieved is a bunch of random float values.

array([[ 0.33519419,  0.53502883,  0.35485002],
   [ 0.73419236,  0.85315716,  0.64135169],
   [ 0.51732791,  0.27411508,  0.32482598]])

I wonder how can I do the same with int

Referring the Official documentation of Numpy for the numpy.random.rand here I don't find a clue on converting or specifying the data type from float values to integer.

How can I achieve random values in int?

1 Answer 1

2

If you want to generate random int, use np.random.randint().

If you want to convert float to int, use np.int32(something).

Sign up to request clarification or add additional context in comments.

4 Comments

Oh Thanks. by the way whats the purpose of numpy.random.rand() function? Is it only to display the float value?
Generate a random float between 0, 1. See documentation docs.scipy.org/doc/numpy-1.13.0/reference/generated/…
Thanks I got it now :-)
The results most are "0" and "1", so how to convert like 0.01231341, 0.2341235. 0.234246524... to 1231341, 2341235, 234246524

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.