2

I am using random.seed to generate pseudo-random numbers over a certain number of iterations. However, with this method, for the same number of iterations, it generates the same initial value each time. I am wondering if there is a way to write the code to generate 4 different random initial values that are in different locations in the parameter range? For example my code looks like this:

import random

N=10
random.seed(N)
vx1 = [random.uniform(-3,3) for i in range(N)]

And each time this will generate the starting vx1[0] = 0.428. Is there a way to write the code for it to generate four different initial values of vx1? So the initial value for vx1 could equal 0.428 or 3 other values. Then each initial value would also have the following 9 random numbers in the range.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
MAJ
  • 437
  • 5
  • 17
  • https://stackoverflow.com/q/22639587/2988730? – Mad Physicist Apr 09 '19 at 15:08
  • 1
    I'm not sure I understand. Calling `seed` with the same value multiple times will cause the random number generator to return the same value multiple times. This is by design. If you don't want the numbers to be the same, don't call `seed` with a static value, don't call it more than once, and possibly, don't call it at all. – Kevin Apr 09 '19 at 15:08
  • To summarize: why are you using `seed()`? – glibdud Apr 09 '19 at 15:15
  • I am currently using seed() right now for Monte Carlo simulation to get a reproducible result with 1,000,000 iterations. However, we think that we may be getting stuck in local variables and not finding the true parameter set that was randomly sampled which is why we think generating 4 random initial parameter sets will allow us to find the maximum. I'm not sure the results will be reproducible without seed – MAJ Apr 09 '19 at 15:27

1 Answers1

2

I think you have a fundamental misunderstanding as to what random.seed does. "Random" number generators are actually deterministic systems that generate pseudo-random numbers. The seed is a label for a reproducible initial state. The whole point of it is that the same sequence of numbers will be generated for the same seed.

If you want to create a reproducible sequence of 1,000,000 numbers, use a seed:

s = 10
N = 1000000

random.seed(s)
vx1 = [random.uniform(-3, 3) for i in range(N)]

If you want to generate a different sequence every time, use a different seed every time. The simplest way to do this is to just not call seed:

N = 1000000

vx1 = [random.uniform(-3, 3) for i in range(N)]
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Okay thanks. I think I am also confused, what does the number `s` affect? For example whats the difference between using s=10 and s=1000000 – MAJ Apr 09 '19 at 15:38
  • @MAJ see https://stackoverflow.com/a/48505649/2166798 for a conceptual explanation of how Python's PRNG works. `s` is being used here as a seed value, which has nothing to do with the sample size N. It's just the entry point into the PRNG's cycle. Same entry point, same sequence gets produced. – pjs Apr 09 '19 at 15:45