Randomizing stimulus duration and equal blocks

OS (e.g. Win10): Win 7
PsychoPy version (e.g. 1.84.x): 1.85.3

Hi everyone.
I’m totally new in world of Python and I really would like to learn this language. But my point is:

  1. I have to make procedure for my research. I have already almost everything without randomizing stimulus duration and a little problem with blocks. It should looks that : Stimulus (random stimulus from base with randomize duration (400, 600, 800, 1000, 1200, 1400, 1600 ms) ) --> rating --> second rating and there should be a loop. I have this without a duration. When I put it in excel worksheet then every stimulus has own constant duration time (I want to randomize it).

  2. Second problem is with blocks because I would like to have five groups of stimulus and every group has to contain equal number of responds for every durations. For example :
    First group has to have 4 responds for 400, 600… ms
    Second group has to have 4 responds for 400,600…ms.

I don’t know, would it be helpful but this is part of my code:

# -------Start Routine "Trening"-------
while continueRoutine and routineTimer.getTime() > 0:
    # get current time
    t = TreningClock.getTime()
    frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
    # update/draw components on each frame
    
    # *image* updates
    if t >= 1 and image.status == NOT_STARTED:
        # keep track of start time/frame for later
        image.tStart = t
        image.frameNStart = frameN  # exact frame index
        image.setAutoDraw(True)
    frameRemains = 1 + time- win.monitorFramePeriod * 0.75  # most of one frame period left
    if image.status == STARTED and t >= frameRemains:
        image.setAutoDraw(False)

I would be grateful for help.