1

I want to create random points between 0.00001 to 100000 and I tried to use the following code

np.random.uniform(0.00001,100000,100)

I have two problems. The first problem is that I would like to have a random-seed like random_state = 123 such that I can replicate my code. The second problem is related to the uniformness of the random.uniform. Despite the name of the function the array does not seem to be drawn from a uniform distribution (see picture).

EDIT: I think I didn't explain the second problem correctly. I wanted my values to be evenly distributed from 1e-5 to 1e+5. The result is evenly distributed but it does not contain very small numbers. That is why I changed it to

temp = 10 ** np.random.uniform(-5,5,100) 

which solved the problem that I intended to solve in the second problem.

enter image description here

wim
  • 338,267
  • 99
  • 616
  • 750
MrYouMath
  • 547
  • 2
  • 13
  • 34
  • 4
    The eye test is not very good for uniformness over such a large interval - look at `np.histogram` with more samples. – miradulo Apr 13 '18 at 22:02
  • 3
    I bet you were expecting a uniform distribution of digit-length, as if rolling a value between 1 and 10 was just as likely as rolling a value between 10000 and 100000. – user2357112 Apr 13 '18 at 22:08

1 Answers1

3

First problem: seeding in numpy is done like this

>>> from numpy.random import RandomState
>>> rs = RandomState(123)
>>> rs.uniform(0.00001,100000,100)[:3]
array([69646.91856282, 28613.93350218, 22685.14536415])
>>> rs.uniform(0.00001,100000,100)[:3]
array([51312.81542477, 66662.45501974, 10590.84851462])
>>> rs.seed(123)  # resetting state of the PRNG
>>> rs.uniform(0.00001,100000,100)[:3]
array([69646.91856282, 28613.93350218, 22685.14536415])

Don't use the numpy.random.seed because it sets a global state. For info on the problems with doing that, check out the comment thread here.

Second problem: Looks uniform enough to me. What's the issue?

wim
  • 338,267
  • 99
  • 616
  • 750
  • plus 1: Thank you for your answer. You solved the random.seed problem. I think I did not express my second problem properly. I 'solved' it and edited my question. – MrYouMath Apr 14 '18 at 09:03
  • 1
    @MrYouMath You shouldn't edit the question, you should post an answer – endolith Dec 07 '18 at 05:30