mercredi 23 septembre 2015

How to get array of random integers of non-default type in numpy

I am generating a 2D array of random integers using numpy:

import numpy
arr = numpy.random.randint(16, size = (4, 4))

This is just an example. The array I am generating is actually enormous and of variable size. Since the numbers are always going to be from 0 to 16, I would like to save some space and have the array be of type uint8. I have tried the following

arr = numpy.random.randint(16, size = (width, height), dtype = numpy.uint8)

in an attempt to match the behavior of zeros and ones, but I get the following error:

Traceback (most recent call last):

File "<ipython-input-103-966a510df1e7>", line 1, in <module>
    maze = numpy.random.randint(16, size = (width, height), dtype = numpy.uint8)

  File "mtrand.pyx", line 875, in mtrand.RandomState.randint (numpy/random/mtrand/mtrand.c:9436)

TypeError: randint() got an unexpected keyword argument 'dtype'

The docs for randint() do not mention anything about being able to set the type. How do I create a random array with a specific integer type? I am not tied to any one function, just a uniform distribution from 0 to 16 of type uint8.




Aucun commentaire:

Enregistrer un commentaire