loop works only for 4 frames any idea why?
Can do this without operators.  I've set script to run on all objects in scene with a name starting with "Cone".

A common blender technique is to select an object and simply use

cone = context.active_object


Here is test code that adds a random z scale for cone on each frame from scene.frame_start to scene.end_frame.

import bpy
from random import randrange
from bpy import context
scene = context.scene
cones = [o for o in scene.objects if o.name.startswith("Cone")]

for cone in cones:
    c = scene.frame_start
    while c &lt;= scene.frame_end:      
        a = randrange(10,30)
        b = randrange(3,6)

        cone.scale.z = a / b
        cone.keyframe_insert("scale", index=2, frame=c)
        c += 1