Twig

The flexible, fast, and secure
template engine for PHP

a Symfony Product
Docs Functions random
You are reading the documentation for Twig 1.x. Switch to the documentation for Twig 2.x. 3.x.

Questions & Feedback

License

Twig documentation is licensed under the new BSD license.

random

New in version 1.5

The random function was added in Twig 1.5.

New in version 1.6

String and integer handling was added in Twig 1.6.

New in version 1.38

The max argument was added in Twig 1.38.

The random function returns a random value depending on the supplied parameter type:

  • a random item from a sequence;
  • a random character from a string;
  • a random integer between 0 and the integer parameter (inclusive).
  • a random integer between the integer parameter (when negative) and 0 (inclusive).
  • a random integer between the first integer and the second integer parameter (inclusive).
1
2
3
4
5
{{ random(['apple', 'orange', 'citrus']) }} {# example output: orange #}
{{ random('ABC') }}                         {# example output: C #}
{{ random() }}                              {# example output: 15386094 (works as the native PHP mt_rand function) #}
{{ random(5) }}                             {# example output: 3 #}
{{ random(50, 100) }}                       {# example output: 63 #}

Arguments

  • values: The values
  • max: The max value when values is an integer
Website powered by Symfony and Twig, deployed on
The Twig logo is © 2010-2021 Symfony