4

I'm testing an algorithm, which has random inputs: for reference, the inputs are a sequence of floating point numbers zipped together with some 0s and 1s (there are relatively few 1s).

In the end, both the probabilities and the position of the 1s will be random. That is proving hard to debug however.

The probabilities are currently distributed according to a Dirichlet distribution:

import numpy.random as ran

N=1024
num_ones = 10
probs = ran.dirichlet([1]*N)
probs = num_ones*probs
probs = probs
probs = sorted(probs, reverse=True)

I realise this may be naive, but I've honestly got no idea how random numbers are generated. How do I make these probabilities the same each time I run the test?

Tom Kealy
  • 2,537
  • 2
  • 27
  • 45

2 Answers2

3

Call

numpy.random.seed(x)

where x is a constant, and you will have same random samples every time you run the program.

Max
  • 3,384
  • 2
  • 27
  • 26
3

You can "seed" the random number generator to make it generate the same numbers on each run.

numpy.random.seed(123) # change the seed for different tests
FogleBird
  • 74,300
  • 25
  • 125
  • 131