python numpy assigning by boolean indexing error "TypeError: array cannot be safely cast to required type"
Question: In the last line of the following code I get an "TypeError: array cannot be
safely cast to required type". Can you help?

Let me explain the code a bit. `randin()` function helps me get an array with
elements in the range specified by `lb` and `ub`. In other words, you give
`randin()` function two one dimensional numpy arrays `lb` and `ub`, and it
returns array `r`, where this inequality holds for all elements `i`: `lb[i] <=
r[i] < ub[i]`

In the one dimensional array`pos` some elements may be out of the range
specified by `lb` and `ub`. I want only those elements to be randomly
reproduced. And I want to use my already existing function `randin()` if
possible. And of course I want to use fancy boolean indexing, not loops.

Thx.

    
    
    import numpy as np
    def randin(lb, ub):
        """return random array with elements between the
        elements of lb and ub. ub is not included."""
        r = ((ub - lb) * np.random.random(np.shape(lb))
            + lb)
        return r
    
    ## inputs
    pos = np.array([1, -3, 5, -7, 9, -11])
    ub = np.ones_like(pos) * 5
    lb = np.zeros_like(pos)
    
    ## reproduce and assign out of range elements
    high = pos > ub
    low = pos < lb
    hl = high | low
    pos[hl] = randin(lb[hl], ub[hl])
    


Answer: When I initialize `pos` like `pos = np.array([1, -3, 5, -7, 9, -11], dtype =
float)` the code runs without problem. So numpy was scared of assigning floats
to integer elements. Not a bad idea.

