Problem with randomize transfom
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)