How to generate random number between 0 and 1 in genetic algorithm toolbox, Deap
<p>I am using <a href="https://pypi.python.org/pypi/deap" rel="nofollow">genetic algorithm toolbox</a> in Python. The code: </p>

<p><code>toolbox.register("attr_bool", random.randint, 0, 1)</code> usually defines random numbers of <code>0</code> and <code>1</code> to be generated. The question is that I am looking for random numbers <strong>between</strong> <code>0</code> and <code>1</code>. I used <code>toolbox.register("attr_bool", random.uniform(0, 1))</code>, but it takes me the below error:
<code>TypeError: the first argument must be callable</code></p>

<p>The error is telling you that it must be able to call the first argument, meaning that it has to be able to act as a function. <code>random.randint</code> is a function, but <code>random.uniform(0, 1)</code> is not. It's a floating-point number. To fix this, you can simply make an anonymous wrapper function with the <code>lambda</code> keyword:</p>

<pre><code>toolbox.register("attr_bool", lambda: random.uniform(0, 1))
</code></pre>

python random genetic-algorithm