The `random` package within `numpy` deals with generating numbers or symbols according to well-known probability functions.
Questions tagged [numpy-random]
74 questions
2
votes
3 answers
How could these alternate numpy `uniform` vs `random` constructions possibly differ?
I had some code that random-initialized some numpy arrays with:
rng = np.random.default_rng(seed=seed)
new_vectors = rng.uniform(-1.0, 1.0, target_shape).astype(np.float32) # [-1.0, 1.0)
new_vectors /= vector_size
And all was working well, all…

gojomo
- 52,260
- 14
- 86
- 115
2
votes
1 answer
Numpy - generate random array with restrictions on individual values in array - Portfolio Optimisation
I'm developing a portfolio optimisation code with constraints using monte-carlo simulation. However, I have run into a problem. My problem is as follows:
I have a list of instruments ["Multi", "Equity 1", "Equity 2", "Equity 3", "FI", "Cash"]
And I…

Dazz W
- 113
- 8
2
votes
2 answers
Why does numpy.random.Generator.choice provides different results (seeded) with given uniform distribution compared to default uniform distribution?
Simple test code:
pop = numpy.arange(20)
rng = numpy.random.default_rng(1)
rng.choice(pop,p=numpy.repeat(1/len(pop),len(pop))) # yields 10
rng = numpy.random.default_rng(1)
rng.choice(pop) # yields 9
The numpy documentation says:
The probabilities…

Andrei
- 961
- 1
- 9
- 29
2
votes
2 answers
randomly choose N elements from each row of 2d array in python
I have a 2d array, say, a = [ [1, 2, 3, 4], [5, 6,7, 8], [9, 10, 11, 12], ...[21, 22, 23, 24] ], and I would like to pick N elements from each row randomly, based on a probability distribution p which can be different for each row.
So basically,…

strugglinggrad
- 47
- 5
2
votes
0 answers
Additive poisson noise to an image
I have written a function to add poisson noise to an image using numpy with np.random.poisson(..). The image is already in numpy array form, using grayscale (0-255). I am wandering if it makes more physical sense to provide the numpy function with…

pierresegonne
- 436
- 1
- 4
- 15
2
votes
2 answers
Split a list into n randomly sized chunks
I am trying to split a list into n sublists where the size of each sublist is random (with at least one entry; assume P>I). I used numpy.split function which works fine but does not satisfy my randomness condition. You may ask which distribution the…

tcokyasar
- 582
- 1
- 9
- 26
2
votes
2 answers
How to use RandomState with Sklearn RandomizedSearchCV on multiple cores
I am puzzled about the right way to use np.random.RandomState with sklearn.model_selection.RandomizedSearchCV when running on multiple cores.
I use RandomState to generate pseudo-random numbers so that my results are reproducible. I give…

hsafer
- 57
- 1
- 9
2
votes
1 answer
Getting closest to endpoints of (1,0) using np.random.uniform
-Random numbers can be drawn on the interval (0, 1], i.e., including 1 using:
import numpy as np
r = np.random.uniform(low=0.0, high=1.0, size=10)
I, however, want to generate numbers on the interval (0, 1), i.e., NOT including 1 and 0. Something…

edager
- 300
- 1
- 8
2
votes
1 answer
Python - Are calls to functions of numpy.random thread safe?
According to this answer, it isn't. But this has not been consistend with what I've observed so far. Consider the following script:
import numpy as np
from multiprocessing.dummy import Pool
from queue import…

spurra
- 1,007
- 2
- 13
- 38
2
votes
1 answer
Fastest way to add noise to a numpy array
I have a numpy.ndarray that represents an image and I would like to add random noise to it. I've done some tests and so far the fastest solution I have is to do:
def RandomNoise(x):
x += np.random.random(x.shape)
But when I have big…

FiReTiTi
- 5,597
- 12
- 30
- 58
2
votes
2 answers
Generating random numbers around a set of coordinates without for loop
I have a set of coordinate means (3D) and a set of standard deviations (3D) accompying them like this:
means = [[x1, y1, z1],
[x2, y2, z2],
...
[xn, yn, zn]]
stds = [[sx1, sy1, sz1],
[sx2, sy2, sz2],
...
…

TheCat
- 137
- 4
2
votes
2 answers
byte array numpy Python 2 vs Python 3
I need to generate tuples of this form : (string, string) or (string, int).
I have the following code which seems to work fine in Python 2 but not returning the desired result in Python 3 (tested on Python 3.5) :
import string
import numpy as…

aviel
- 178
- 9
2
votes
2 answers
How to randomize all the items in a matrix in python
I'm trying to create a function that accepts a matrix and assigns random variables to each item of said matrix using python.
It seems rather simple but I can't seem to get it to work. The two closest tries Ive done were:
def MatrixRandomize(v):
…

Debo Akeredolu
- 140
- 1
- 8
1
vote
2 answers
Uniformity test on a large set using scipy.stats.kstest
I'm playing around with some hardware entropy creation. I'm using electronic sensors to sample the environment, using A2D with between 12-16 bits of resolution. I'm hypothesizing that the lower-order bits from these readings contain enough noise and…

cbp2
- 245
- 1
- 3
- 12
1
vote
1 answer
Numpy's Multivariate Normal example is not clear
I am not sure if this question fits to numpy users or mathematicians. I don't understand how the numpy.random.multivariate_normal's example works.
In the bottom of the documentation, it generates a few random values given a mean and covariance…

Shayan
- 125
- 2
- 13