This might be a silly question but I haven't been able to find a function to generate an array of random floats of a given length between a certain range.
I've looked at Random sampling but no function seems to do what I need.
random.uniform comes close but it only returns a single element, not a specific number.
This is what I'm after:
ran_floats = some_function(low=0.5, high=13.3, size=50)
which would return an array of 50
random non-unique floats (ie: repetitions are allowed) uniformly distributed in the range [0.5, 13.3]
.
Is there such a function?
There may already be a function to do what you're looking for, but I don't know about it (yet?). In the meantime, I would suggess using:
This will produce an array of shape (50,) with a uniform distribution between 0.5 and 13.3.
You could also define a function:
EDIT: Hmm, yeah, so I missed it, there is numpy.random.uniform() with the same exact call you want! Try
import numpy; help(numpy.random.uniform)
for more information.The for loop in list comprehension takes time and makes it slow. It is better to use numpy parameters (low, high, size, ..etc)
sample output:
('it took: ', 0.06406784057617188)
('it took: ', 1.7253198623657227)
Why not use a list comprehension?
np.random.uniform
fits your use case: http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.uniform.htmlWhy not to combine random.uniform with a list comprehension?
Alternatively you could use SciPy
and for the record to sample integers it's