# random letter generator

**URL:** <https://community.f5.com/t/random-letter-generator/67120>\
**Category:** Community CodeShare\
**Tags:** irules, utility, devops\
**Created:** [March 18, 2015, 9:30pm UTC](https://community.f5.com/t/random-letter-generator/67120 "2015-03-18T21:30:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [March 18, 2015, 9:30pm UTC](https://community.f5.com/t/random-letter-generator/67120/1 "2015-03-18T21:30:36Z")

</div>

**Problem this snippet solves:**

This iRule demonstrates how to generate a string of pseudo-random letters. rand() doesn’t generate cryptographically secure output. If you need this, use the [CRYPTO](https://devcentral.f5.com/Wiki/iRules.CRYPTO.ashx) commands.

**Code :**

```auto
when RULE_INIT {

   # Number of random letters to generate
   set count 100

   # Create a list of the letters indexed 0 through 25
   set letters [list a b c d e f g h i j k l m n o p q r s t u v w x y z]

   # Initialize a variable to store the random letters in
   set random ""

   # Loop through X times where X is the number of random letters to generate
   for { set i 1 } { $i < $count } { incr i } {

      # Generate a random number between 0 and 1, using rand()
      # Multiply that by 26 to get a number representing a letter
      # Use int to trim off the decimal value

      # set rand [expr { int (rand() * 26) }]
      # append random [lindex $letters $rand]

      # Or in one command:
      append random [lindex $letters [expr { int (rand() * 26) }]]
   }

   log local0. "Random letters: $random"
}

```
