How can i create a random array of floats from 0 to 5 in python

Dave Angel davea at davea.name
Tue Mar 12 16:52:35 EDT 2013


On 03/12/2013 01:11 PM, Norah Jones wrote:
> I want to create a random float array of size 100, with the values in the array ranging from 0 to 5. I have tried random.sample(range(5),100) but that does not work. How can i get what i want to achieve?
>
>

None of the responses so far actually give you what you asked for, as 
they assume you didn't mean 'array' but meant 'list.'  I suspect they're 
right, but here's an approach for array.array.

If you really want a multiprocess.Array, or numpy's array, please say 
so, and somebody'll tell you how to put random numbers in one of them.



import array
import random

floats = (random.random() * 5 for _ in xrange(100))
data = array.array('d', floats)

print data
print type(data)



-- 
DaveA



More information about the Python-list mailing list