generating random values based on condition in structured array python
Question: I have an array representing a position (x,y) and colors (r,g,b). Right now
the array turns all zeros (as it should). I need to make it so that the values
of x and y are random and <=10. I need to make the values of r,g, and b to
also be random but <=255. How do you specify to only generate to 10 for
'position' and likewise to 255 for 'color'.

    
    
    array = np.zeros(10, [ ('position', [ ('x', float, 1),
                                       ('y', float, 1)]),
                        ('color',    [ ('r', float, 1),
                                       ('g', float, 1),
                                       ('b', float, 1)])])
    


Answer: Try this:

    
    
    import random 
    import numpy
    
    array = numpy.zeros(10, [ ('position', [ ('x', float, 1),
                                       ('y', float, 1)]),
                        ('color',    [ ('r', float, 1),
                                       ('g', float, 1),
                                       ('b', float, 1)])])
    
    
    ranPos = lambda: random.random() * 10
    ranColor = lambda: random.random() * 255
    
    for i in range(10):
        array[i] = ((ranPos(), ranPos()),
                    (ranColor(), ranColor(), ranColor()))
    

If you want to generate integers use this instead:

    
    
    ranPos = lambda: random.randint(0,10)
    ranColor = lambda: random.randint(0,255)
    

