Have some code. Share some code.
Showing results for 
Search instead for 
Did you mean: 

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 commands.

Code :

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"
Version history
Last update:
‎18-Mar-2015 14:30
Updated by: