Random.choice always slects the same values

Hello,

I am currently facing an issue with obtaining different random values using the random.choice(Car_models)in my code. The problem is that on each frame, it consistently provides me with the same cars from the list.

Here is the relevant portion of the code:

cars =

def car(num_cars, choicesList, lane_orientation):

for i in range(num_cars):
       
    # Randomly select a car model from the list
    car_model_path = random.choice(CAR_MODELS)
    # Create an instance from the random car
    car_instance = rep.create.from_usd(car_model_path, semantics=[('class', 'car')])      
    # Position instances       
    with car_instance:
        rep.modify.pose(
            position=rep.distribution.choice(choicesList, with_replacements=False),
            rotation=rep.distribution.uniform(lane_orientation[0], lane_orientation[1]),                  
        )                
        cars.append(car_instance)
             
return cars 

rep.randomizer.register(car)

Also in this part of the code:

position=rep.distribution.choice(choicesList, with_replacements=False),

While randomization appears to be functioning on each frame, it often selects the same value, despite the with_replacements=False argument that should prevent this.

I attempted to replace random.choice(car_models) with rep.distribution.choice(car_models), as the latter function randomizes every frame. However, upon making this change, the code ceased to function.

I would greatly appreciate any insights or guidance on resolving this issue. Thank you in advance for your assistance.

Best regards,
Ivan

Hello Ivan,

There is a bit of a mis-understanding happening with creating a custom randomizer. Essentially the code only gets executed once (at instantiation) which sets values and creates replicator execution graphs. This is why Python’s random.choice is only executed once, and not every frame. Only the replicator functions are executed in triggers.

See this example in our tutorials using instantiate

Modified with choice for the position, it would look something like this

import omni.replicator.core as rep

with rep.new_layer():
    POSITION_CHOICES = [(-400, 0, -400), (400, 0, -400), (400,0,400), (-400,0,400)]
    # Add Default Light
    distance_light = rep.create.light(rotation=(315,0,0), intensity=3000, light_type="distant")

    PROPS = 'omniverse://localhost/NVIDIA/Assets/Vegetation/Plant_Tropical/'
    plane = rep.create.plane(scale=10, visible=True)

    def get_props(size):
        instances = rep.randomizer.instantiate(rep.utils.get_usd_files(PROPS, recursive=True), size=size, mode='point_instance')
        with instances:
            rep.modify.pose(
                position=rep.distribution.choice(POSITION_CHOICES),
                rotation=rep.distribution.uniform((-90, -180, 0), (-90, 180, 0)),
            )
        return instances.node

    rep.randomizer.register(get_props)

    # Setup randomization
    with rep.trigger.on_frame(num_frames=10):
        rep.randomizer.get_props(2)