8000 random_sample for type float32 · Issue #3155 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

random_sample for type float32 #3155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dstuebe opened this issue Mar 17, 2013 · 5 comments
Closed

random_sample for type float32 #3155

dstuebe opened this issue Mar 17, 2013 · 5 comments

Comments

@dstuebe
Copy link
dstuebe commented Mar 17, 2013

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
@rkern
Copy link
Member
rkern commented Mar 17, 2013

In the meantime, you can do the following:

import numpy as np

f32max = 1 << np.finfo(np.float32).nmant

def random_sample_f32(size=None):
    sample = np.empty(size, dtype=np.float32)
    sample[...] = np.random.randint(0, f32max, size=size) / np.float32(f32max)
    if size is None:
        sample = sample[()]
    return sample

@dstuebe
Copy link
Author
dstuebe commented Mar 17, 2013

Thanks rKern
Modified slightly to take the type as an argument but default to f64.

Can someone take up extending the current interface? While we are in there though, why don't the random functions provide an optional out argument in which to pass an allocated buffer? I would be willing to put in some work to do it if an extended api can be agreed upon by the group?

import numpy as np

def random_sample(size=None,dtype=np.float64):

    type_max = 1 << np.finfo(dtype).nmant
    sample = np.empty(size, dtype=dtype)
    sample[...] = np.random.randint(0, type_max, size=size) / dtype(type_max)
    if size is None:
        sample = sample[()]
    return sample

@waylonflinn
Copy link

The following adds support for strings in the dtype argument. This is a common pattern found throughout numpy. Examples can be found below.

Code

import numpy as np

def random_sample(size=None, dtype=np.float64):

	if type(dtype) == str:
		dtype = np.dtype(dtype).type

	type_max = 1 << np.finfo(dtype).nmant
	sample = np.empty(size, dtype=dtype)
	sample[...] = np.random.randint(0, type_max, size=size) / dtype(type_max)
	if size is None:
		sample = sample[()]
	return sample

Example Usage

random_sample([10], dtype="float32")

Similar To

  • np.random.randint(0, 3, 100, dtype="int32")
  • np.ndarray(10, dtype="float32")

@bashtage
Copy link
Contributor

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

@mattip
Copy link
Member
mattip commented May 29, 2019

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

@mattip mattip closed this as completed May 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants
0