title: Extensible biased number generator - Python
body: <p>I am trying to get a random number generator that will be biased in that it takes a number, and prints a number that is likely to be close. Here's what I have now:</p>
  
  <pre><code>def biasedRandom(rangen, rangex, target, biaslevel=1):
      if rangen &gt; rangex:
          raise ValueError("Min value is less than max value.")
          return
      if not target in range(rangen, rangex):
          raise ValueError("Bias target not inside range of random.")
          return
  
      num = random.randint(rangen, rangex)
      for i in range(biaslevel):
          distance = abs(num - target)
          num -= random.randint(0, distance)
  
      return num
  </code></pre>
  
  <p>This works pretty well, however it has on occasion given completely outrageous numbers; e.g. it once gave <code>-246174068358</code> for <code>(1,100,30,60)</code>. I figure there is just a bug in there that I am not seeing.</p>
  
  <p>Thanks in advance.</p>