question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

random_sample for type float32

See original GitHub issue

Hi 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:closed
  • Created 11 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mattipcommented, May 29, 2019

Closing, the new API supports dtype, note the name is now np.random.Generator().random not random_sample

1reaction
bashtagecommented, Apr 11, 2019

xref #13163 . This is fixed in the randomgen branch.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found