擅长:python、mysql、java
<p>raise退出函数-您不需要跟随raise和return</p>
<p>范围内的目标(lo,hi)效率低下;为什么不lo<;=target<;hi?在</p>
<p><strong>编辑:</strong></p>
<pre><code>import random
def biasedRandom(lo, hi, target, steps=1):
if lo >= hi:
raise ValueError("lo should be less than hi")
elif target < lo or target >= hi:
raise ValueError("target not in range(lo, hi)")
else:
num = random.randint(lo, hi)
for i in range(steps):
num += int(random.random() * (target - num))
return num
</code></pre>
<p>随着步骤的增加,这将非常迅速地集中到目标上;您可能需要进行一些试用发行版,以确保您获得了预期的结果,或者尝试使用随机高斯相反。在</p>