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.

Feature request: numpy.random.normal with `dtype`

See original GitHub issue

When constructing arrays it is useful to set the dtype. For some reason we can do it with some np.random methods such as random integers but not with random normal values. Could we have a consitent version across all methods that generate new np.arrays?

>>> np.random.randint(10,dtype="Int16")
9
>>> np.random.normal(10,dtype="Float32")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mtrand.pyx", line 1547, in mtrand.RandomState.normal (numpy/random/mtrand/mtrand.c:20493)
TypeError: normal() got an unexpected keyword argument 'dtype'

This would be nice in order to avoid np.array( somearray, dtype=sometype ) every time we create a new random array.

For example

np.array( np.random.normal(10), dtype="Float32")

could be simply written as

 np.random.normal(10, dtype="Float32")

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
bashtagecommented, Jan 28, 2019

Use randomgen until the new PRNG infrastructure gets into NumPy (hopefully this year). 32 bit is supported for the most common types (normal, exponential, uniform and gamma).

2reactions
lucasb-eyercommented, Jan 28, 2019

I would like to re-open the issue, as I keep bumping into wanting this almost weekly for over a year. The conversion is not a solution, because most of the times I need this it is for memory purposes. It seems to be a somewhat common request, see for example this SO question about it (not me), and the proposed workaround.

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.random.normal — NumPy v1.24 Manual
Draw random samples from a normal (Gaussian) distribution. The probability density function of the normal distribution, first derived by De Moivre and 200...
Read more >
Supported NumPy features - Numba
NumPy dtypes provide type information useful when compiling, and the regular, ... Numba supports top-level functions from the numpy.random module, ...
Read more >
tf.random.normal | TensorFlow v2.11.0
Outputs random values from a normal distribution. ... Watch on demand ... mean, A Tensor or Python value of type dtype , broadcastable...
Read more >
Can I specify a numpy dtype when generating random values?
numpy will automatically convert the type of your random array to the type of x when you do the operation in-place, there's absolutely...
Read more >
Chapter 4. NumPy Basics: Arrays and Vectorized Computation
Since NumPy is a large topic, I will cover many advanced NumPy features like ... function in numpy.random to generate some random normally...
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