Newbie How to include Jinja inside script

I have tried several attempts to get my template which works in the template editor inside a new Script
Aim is to make a random number then use a if else to choose scripts (I can use code but cannot get it to work)

alias: test
sequence: 
  {% set randomNumber = range(1, 3) | random %}
  {% if 6 == 1 %}
  - service: script.iron_man_on
  {% elif 2 == 2 %} 
  - service: script.arc_reactor_on
  {% else %}
  - service: script.iron_man_on
  - service: script.arc_reactor_on
  {% endif %}

mode: single
icon: mdi:alarm-light

tried

bob:
alias: test
  sequence: 
    - service: script.turn_on
    data_template:
      {% set randomNumber = range(1, 3) | random %}
      {% if 6 == 1 %}
      - service: script.iron_man_on
      {% elif 2 == 2 %} 
      - service: script.arc_reactor_on
      {% else %}
      - service: script.iron_man_on
      - service: script.arc_reactor_on
      {% endif %}

mode: single
icon: mdi:alarm-light

I would like to use Jinja as I am a programmer.

Many Thanks

There are a lot of issues with your script…

When you have a lot of possible choices, it’s better to use a mapping method instead of a bunch of “if/elifs”. For just a few choices, something like this will work:

alias: test
sequence:
  - service: script.turn_on
    data: {}
    target:
      entity_id: "{{ random_script }}"
mode: single
icon: mdi:alarm-light
variables:
  random_script: >
    {% set randomNumber = range(1, 4) | random %}
    {% if randomNumber == 1 %}
      script.iron_man_on
    {% elif randomNumber == 2 %}
      script.arc_reactor_on
    {% elif randomNumber == 3 %}
      script.iron_man_on, script.arc_reactor_on
    {% endif %}

But, if you don’t need the randomNumber variable for something else, you could also just use the | random filter on the options:

alias: test
mode: single
icon: mdi:alarm-light
sequence:
  - service: script.turn_on
    data: {}
    target:
      entity_id: "{{ ('script.iron_man_on','script.arc_reactor_on', 'script.iron_man_on, script.arc_reactor_on')|random }}"

EDIT: Fixed C/P error

Thank you for your response, it does not like two scripts with the comma in between:

{% elif randomNumber == 3 %}
  script.iron_man_on, script.arc_reactor_on
{% endif %}

Error is template rendered invalid service

Am I doing something wrong?

Thanks

Combined both answers of yours this works thank you

alias: Bedroom_BedLight_Random
sequence:
  - service: script.turn_on
    target:
      entity_id: |
        {% set randomNumber = range(1, 4) | random %} {% if randomNumber == 1 %}
          script.iron_man_on
        {% elif randomNumber == 2 %}
          script.arc_reactor_on
        {% elif randomNumber == 3 %}
          script.iron_man_on, script.arc_reactor_on
        {% endif %}
mode: single
icon: mdi:alarm-light