Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41150

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

Date 2013-03-12 16:52 -0400
From Dave Angel <davea@davea.name>
Subject Re: How can i create a random array of floats from 0 to 5 in python
References <abcd1234abc123ab12a0000000028000020000001052@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3251.1363121577.2939.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: How can i create a random array of floats from 0 to 5 in python Dave Angel <davea@davea.name> - 2013-03-12 16:52 -0400

csiph-web