random_sample for type float32
See original GitHub issueHi Numpy I need a numpy.float32 array with a distribution between [0…1). I can not find a way to generate this array using the existing numpy.random tools as converting from the default double to float causes the distribution to change to [0…1].
You can see the problem in the code below. Can you suggest a better approach or should an optional argument for dtype be added to the existing methods?
Thanks David
import numpy
sz = 10000000
rnd_f32 = numpy.zeros(sz,numpy.float32)
while True:
rnd_f64 = numpy.random.rand(sz) # Creates doubles!
rnd_f32[:] = rnd_64 #cast to float32
if (rnd_f32 >= 1.0).any():
print "FooBar!"
print "Rnd64 Max: %s" % max(rnd_f64)
print "Rnd32 Max: %s" % max(rnd_f32)
break
Issue Analytics
- State:
- Created 11 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Generate Random Float numbers in Python using ... - PYnative
In this lesson, you will learn how to generate a random float number in Python using random() and uniform() functions of a random...
Read more >numpy.random.random_sample — NumPy v1.24 Manual
Return random floats in the half-open interval [0.0, 1.0). Results are from the “continuous uniform” distribution over the stated interval. To sample ......
Read more >How to get a random number between a float range?
Use random.uniform(a, b): >>> import random >>> random.uniform(1.5, 1.9) 1.8733202628557872.
Read more >4 Ways to Perform Random Sampling in NumPy - AskPython
Again, ranf() function resembles random_sample() method in terms of functioning. It generates random numbers of type float between 0.0 to 1.0 only. Example:....
Read more >Random sampling from a list in Python (random.choice ...
The type of the list element specified in weights can be either int or float . If set to 0 , the element...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Closing, the new API supports dtype, note the name is now
np.random.Generator().random
notrandom_sample
xref #13163 . This is fixed in the randomgen branch.