title: Small random values in range [0,1], non-uniform, on log scale for GA? in Python
body: <p>I recreated the initialisation and mutation of a Genetic Algorithm and found that if I have a min and max of 0.001 and 1 for the parameters of a chromosome, and do random.uniform(min, max), the probability that a random value lies between [0.1, 1] is ~0.9, and between [0.01, 0.1] is ~0.09, and between [0.001, 0.01] is ~0.009 and so on.. Am I correct on this one?</p>
  
  <p>The same happens for random.randint(1, 1000)/1000.0. This is because both are uniformly distributed right?</p>
  
  <p>This makes sense, but in my opinion it is not fair that a new initialised value is more likely to have a greater value, in case of the GA. The probability should be equal between [0.1, 1.0] and [0.01, 0.1]. </p>
  
  <p>I found random number generators with different distribution functions. But it looks that none of them do what I think it should do. (numpy.random.lognormal, random.lognormvariate). Maybe some of them will fit, but I have no idea how to set the mu and sigma to scale between my min and max. Can someone help?</p>
  
  <p>Some code that plots the distribution as a histogram:</p>
  
  <pre><code>import random
  import numpy as np
  min = 0.001
  max = 1.0
  s = []
  for i in range(100000):
      s.append(random.uniform(min, max))
  
  #for i in range(100000):
  #    s.append(random.lognormvariate(0.00, 0.5))
  
  print(s)
  import matplotlib.pyplot as plt
  count, bins, ignored = plt.hist(s, 500, normed=True)
  plt.plot(bins, np.ones_like(bins), linewidth=2, color='r')
  plt.show()
  </code></pre>
  
  <p>Edit:
  Then this would be the solution, it looks good.</p>
  
  <pre><code>import random
  import numpy as np
  min = 0.001
  max = 1.0
  s = []
  #for i in range(100000):
  #    s.append(random.uniform(min, max))
  for i in range(100000):
      s.append(10 ** random.uniform(np.log10(min), np.log10(max)))
  #for i in range(100000):
  #    s.append(random.lognormvariate(0.00, 0.5))
  np.log
  print(s)
  import matplotlib.pyplot as plt
  count, bins, ignored = plt.hist(s, 500, normed=True)
  plt.plot(bins, np.ones_like(bins), linewidth=2, color='r')
  plt.show()
  </code></pre>