How to copy objects and set their origins with Python console? I am trying to copy Cube and set different random origins to the copies.
I use the following code:
import bpy
import random

C = bpy.context
src_obj = bpy.data.objects['Cube']


for i in range (0,5):
    new_obj = src_obj.copy()
    new_obj.data = src_obj.data.copy()
    new_obj.animation_data_clear()
    C.collection.objects.link(new_obj)
    new_origin = (0.0+random.randint(1, 9)/100,0.0+random.randint(1, 9)/100,0.0+random.randint(1, 9)/100)
    print(new_origin)
    C.scene.cursor.location = new_origin
    new_obj.select_set(True)
    bpy.ops.object.origin_set(type='ORIGIN_CURSOR')

The objects are successfully copied, but their origins are always the same: they are equal to last generated coordinates. I added print to make sure that new_origin is always different, but the last value of new_origin is applied. What I am doing wrong? How to make the origins to be different?
The reason all of them use the same location is because you are leaving them all selected.
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')

will set the object origin to ORIGIN_CURSOR for all selected objects.
Add:
new_obj.select_set(False)

as the last line in your for loop.