Problem with randomize transfom first of all, i have absolutely no clue what i am doing here. A whole day of tutorials and try and error.
I want to make the cubes move randomly on the z-axis in a fixed range (loc=0,0,2) and write keyframes every 5 frames.
The Problem: How can i tell the operator to calculate always from the same location?
e.g.: The Cube is at location (0,0,0), the operator starts by (0,0,0) the keyframe is inserted with a location of (0,0,x). The operator starts again, but unfortunately by (0,0,x) and not by (0,0,0).

Best Regards,
Rollo
import bpy
from random import randint

frame_number=0

for obj in bpy.data.collections['Cubes'].all_objects:
    obj.select_set(True)
    
for i in range(0,50):
    x = randint(0,1000)
    bpy.context.scene.frame_set(frame_number)
    bpy.ops.object.randomize_transform(random_seed=x, loc=(0, 0, 2))
    bpy.ops.anim.keyframe_insert_menu(type='Location')
    frame_number +=5
Same thing using API methods.
Instead of using the operator calls can individually set each cube's z location to a random float value between 0 and 2, and insert a keyframe, in this case Z location at a designated frame. Notice not changing frame.
import bpy
from bpy import context
from random import uniform

#col = contxxt.collection
col = bpy.data.collections.get(&quot;Cubes&quot;)
if col:   
    for frame_number in range(0 ,50, 5):
        for o in col.all_objects:
            o.location.z = uniform(0, 2)
            o.keyframe_insert(&quot;location&quot;, index=2, frame=frame_number) # z axis only

A transform, moves an object from where it is plus an amount, would be same as using
o.location.z += uniform(0, 2)

To have the value always zero on frame 0 could use
o.location.z = uniform(0, 2) if frame else 0

To insert all location keyframes, omit the index argument of Object.keyframe_insert
o.keyframe_insert(&quot;location&quot;, frame=frame_number)