[Numpy-discussion] array of random numbers fails to construct

Allan Haldane allanhaldane at gmail.com
Tue Dec 8 20:04:54 EST 2015


On 12/08/2015 08:01 PM, Allan Haldane wrote:
> On 12/08/2015 07:40 PM, Stephan Hoyer wrote:
>> On Sun, Dec 6, 2015 at 3:55 PM, Allan Haldane <allanhaldane at gmail.com
>> <mailto:allanhaldane at gmail.com>> wrote:
>>
>>
>>     I've also often wanted to generate large datasets of random uint8
>>     and uint16. As a workaround, this is something I have used:
>>
>>     np.ndarray(100, 'u1', np.random.bytes(100))
>>
>>     It has also crossed my mind that np.random.randint and
>>     np.random.rand could use an extra 'dtype' keyword. It didn't look
>>     easy to implement though.
>>
>>  
>> Another workaround that avoids creating a copy is to use the view
>> method, e.g.,
>> np.random.randint(np.iinfo(int).min, np.iinfo(int).max,
>> size=(1,)).view(np.uint8)  # creates 8 random bytes
> 
> Just to note, the line I pasted doesn't copy either, according to the
> OWNDATA flag.
> 
> Cheers,
> Allan

Oops, but I forgot my version is readonly. If you want to write to it
you do need to make a copy, that's true.

Allan




More information about the NumPy-Discussion mailing list