random.rst 803B

12345678910111213141516171819202122232425
  1. ``random``
  2. ==========
  3. .. versionadded:: 1.5
  4. The random function was added in Twig 1.5.
  5. .. versionadded:: 1.6
  6. String and integer handling was added in Twig 1.6.
  7. The ``random`` function returns a random value depending on the supplied
  8. parameter type:
  9. * a random item from a sequence;
  10. * a random character from a string;
  11. * a random integer between 0 and the integer parameter (inclusive).
  12. .. code-block:: jinja
  13. {{ random(['apple', 'orange', 'citrus']) }} {# example output: orange #}
  14. {{ random('ABC') }} {# example output: C #}
  15. {{ random() }} {# example output: 15386094 (works as native PHP `mt_rand`_ function) #}
  16. {{ random(5) }} {# example output: 3 #}
  17. .. _`mt_rand`: http://php.net/mt_rand