1

I am using genetic algorithm toolbox in Python. The code:

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

martineau
  • 119,623
  • 25
  • 170
  • 301
Amn Kh
  • 531
  • 3
  • 7
  • 19

1 Answers1

2

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. random.randint is a function, but random.uniform(0, 1) is not. It's a floating-point number. To fix this, you can simply make an anonymous wrapper function with the lambda keyword:

toolbox.register("attr_bool", lambda: random.uniform(0, 1))
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97