Questions tagged [numpy-random]

The `random` package within `numpy` deals with generating numbers or symbols according to well-known probability functions.

74 questions
98
votes
3 answers

Differences between numpy.random.rand vs numpy.random.randn in Python

What are the differences between numpy.random.rand and numpy.random.randn? From the documentation, I know the only difference between them is the probabilistic distribution each number is drawn from, but the overall structure (dimension) and data…
Phúc Lê
  • 2,405
  • 3
  • 17
  • 14
21
votes
7 answers

What numbers that I can put in numpy.random.seed()?

I have noticed that you can put various numbers inside of numpy.random.seed(), for example numpy.random.seed(1), numpy.random.seed(101). What do the different numbers mean? How do you choose the numbers?
Shelly
  • 569
  • 2
  • 5
  • 7
9
votes
3 answers

How to access numpy default global random number generator

I need to create a class which takes in a random number generator (i.e. a numpy.random.RandomState object) as a parameter. In the case this argument is not specified, I would like to assign it to the random generator that numpy uses when we run…
Atimaharjun
  • 349
  • 2
  • 10
8
votes
1 answer

How to use the new NumPy random number generator?

The fact that NumPy now recommends that new code uses the defacult_rng() instance instead of numpy.random for new code has got me thinking about how it should be used to yield good results, both performance vice and statistically. This first…
Marcus
  • 401
  • 6
  • 13
8
votes
3 answers

How to create a random array in a certain range

Suppose I want to create a list or a numpy array of 5 elements like this: array = [i, j, k, l, m] where: i is in range 1.5 to 12.4 j is in range 0 to 5 k is in range 4 to 16 l is in range 3 to 5 m is in range 2.4 to 8.9. This is an example to…
Clement Attlee
  • 723
  • 3
  • 8
  • 16
7
votes
1 answer

What is the difference between numpy.random's Generator class and np.random methods?

I have been using numpy's random functionality for a while, by calling methods such as np.random.choice() or np.random.randint() etc. I just now found about the ability to create a default_rng object, or other Generator objects: from numpy.random…
johannesack
  • 660
  • 3
  • 19
4
votes
3 answers

Why this small snippet hangs using multiprocessing with maxtasksperchild, numpy.random.randint and numpy.random.seed?

I have a python script that concurrently processes numpy arrays and images in a random way. To have proper randomness inside the spawned processes I pass a random seed from the main process to the workers for them to be seeded. When I use…
3
votes
1 answer

`numpy.random.normal` generates different numbers on different systems

I'm comparing the numbers generated with np.random.normal on two different systems (details see below) with the following code (I'm using the legacy np.random.seed because this is used by another program whose output I want to verify eventually) (1)…
a_guest
  • 34,165
  • 12
  • 64
  • 118
3
votes
2 answers

1D Wasserstein distance in Python

The formula below is a special case of the Wasserstein distance/optimal transport when the source and target distributions, x and y (also called marginal distributions) are 1D, that is, are vectors. where F^{-1} are inverse probability distribution…
develarist
  • 1,224
  • 1
  • 13
  • 34
3
votes
1 answer

Why is np.random.default_rng().permutation(n) preferred over the original np.random.permutation(n)?

Numpy documentation on np.random.permutation suggests all new code use np.random.default_rng() from the Random Generator package. I see in the documentation that the Random Generator package has standardized the generation of a wide variety of…
David Parks
  • 30,789
  • 47
  • 185
  • 328
3
votes
2 answers

Direct way to access Numpy RandomState object

Is there are more direct way to access the RandomState object created on import other than np.random..__self__? Both np.random._rand and getattr(np.random, "_rand") raise AttributeError. The former works fine but doesn't seem very…
deasmhumnha
  • 456
  • 4
  • 12
3
votes
2 answers

Randomly selecting from Pandas groups with equal probability -- unexpected behavior

I have 12 unique groups that I am trying to randomly sample from, each with a different number of observations. I want to randomly sample from the entire population (dataframe) with each group having the same probability of being selected from. The…
pyrate
  • 33
  • 4
3
votes
1 answer

Converting to and from numpy's np.random.RandomState and Python's random.Random?

I would like to be able to convert back and forth between Python's standard Random and numpy's np.random.RandomState. Both of these use the Mersenne Twister algorithm, so it should be possible (unless they are using different versions of this…
Erotemic
  • 4,806
  • 4
  • 39
  • 80
3
votes
1 answer

Zipf Distribution: How do I measure Zipf Distribution using Python / Numpy

I have a file (lets say corpus.txt) of around 700 lines, each line containing numbers separated by -. For example: 86-55-267-99-121-72-336-89-211 59-127-245-343-75-245-245 First I need to read the data from the file, find the frequency of each…
RDM
  • 1,136
  • 3
  • 28
  • 50
2
votes
0 answers

How to create NumPy random 2D array with full rank, or a particular rank?

In NumPy, I can use random package to create a 2D array, but cannot make sure it has full rank or a particular rank. How to got it? In the case of full rank, I can use linalg.matrix_rank to check, but I want to make sure this in more simple way. A…
ydhhat
  • 381
  • 2
  • 15
1
2 3 4 5