Showing random logos in a custom module using image repeater field

Finally I was able to resolve the issue related to variable not retaining its value outside the loop by using append method. You can see the code below:

{% set logo_images = module.logo_image %}
{% set total_logos = logo_images|length %}
{% set num_to_select = 4 %}
{% set selected_indices = [] %}

{% if total_logos < num_to_select %}
{% set num_to_select = total_logos %}
{% endif %}

{% for i in range(num_to_select) %}
{% set random_index = range(total_logos)|random %}
{% if random_index not in selected_indices %}
{% do selected_indices.append(random_index) %}
{% else %}
{% set random_index = range(total_logos)|random %}
{% do selected_indices.append(random_index) %}
{% endif %}
{% endfor %}

{% for index in selected_indices %}
{% set logo = logo_images[index] %}
{% if logo.src %}
{% set sizeAttrs = ‘width=“{{ logo.width }}” height=“{{ logo.height }}”’ %}
{% if logo.size_type == ‘auto’ %}
{% set sizeAttrs = ‘width=“{{ logo.width }}” height=“{{ logo.height }}” style=“max-width: 100%; height: auto;”’ %}
{% elif logo.size_type == ‘auto_custom_max’ %}
{% set sizeAttrs = ‘width=“{{ logo.max_width }}” height=“{{ logo.max_height }}” style=“max-width: 100%; height: auto;”’ %}
{% endif %}
{% set loadingAttr = logo.loading != ‘disabled’ ? ‘loading=“{{ logo.loading }}”’ : ‘’ %}
<img src=“{{ logo.src }}” alt=“{{ logo.alt }}” {{ loadingAttr }} {{ sizeAttrs }}>
{% endif %}
{% endfor %}
I believe this thread will be useful to others.