generate random Strings in AS2 or AS3

I was searching for a function to generate random Strings either in AS2 or in AS3 but I couldn’t find any so I made my own using code from a typewriter effect, but I can’t seem to find the page anymore. I use this code when I load dynamic content from php and such and I don’t want flash to cache my request. Here is an ActionScript 2 version of the code:

1
2
3
4
5
6
7
8
9
function generateRandomString(newLength:Number):String{
  var a:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  var alphabet:Array = a.split("");
  var randomLetter:String = "";
  for (var i:Number = 0; i < newLength; i++){
    randomLetter += alphabet[Math.floor(Math.random() * alphabet.length)];
  }
  return randomLetter;
}

For the ActionScript 3 version of it I made some optimizations and I created a class with the static method in it here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
package net.zedia.utils{
  public final class StringUtils{
    public static function generateRandomString(newLength:uint = 1, userAlphabet:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"):String{
      var alphabet:Array = userAlphabet.split("");
      var alphabetLength:int = alphabet.length;
      var randomLetters:String = "";
      for (var i:uint = 0; i < newLength; i++){
        randomLetters += alphabet[int(Math.floor(Math.random() * alphabetLength))];
      }
      return randomLetters;
    }
  }
}

here is how you use it:

1
2
3
4
import net.zedia.utils.StringUtils;
 
trace (StringUtils.generateRandomString(4));//for a random string of 4 characters
trace (StringUtils.generateRandomString(4, "ROGER"));//for a random string of 4 characters using only the following letters: ROGER

Cet article en Français

, , ,

  1. #1 by Daniel - June 10th, 2008 at 13:59

    Hey, this is super helpful, thanks :)

  2. #2 by David Patrick - June 13th, 2008 at 17:48

    Thank you sooo much, you’re the best !!!!!

  3. #3 by Michiel van der Ros - October 20th, 2008 at 11:02

    Ah, this saves me some work. Thanks for putting it online!

  4. #4 by Defmech - October 23rd, 2008 at 06:51

    Thanks for that. Very handy. Already started work extending it to generate capitalised words. Next step phrases and Paragraphs. :)

  5. #5 by Michal K - March 2nd, 2009 at 01:48

    Awesome, totally gonna use it for my project. Thanks!

  6. #6 by free_lady - April 16th, 2009 at 03:56

    thanks for this

  7. #7 by chris - June 18th, 2009 at 04:59

    It works like a charm.

    Thanks

  8. #8 by sagar - June 19th, 2009 at 08:17

    Hi,

    This is gr8!
    but am trying for a method which takes a character as a parameter generateRandomString(5, ‘R’), which returns a string has no ‘R’ as well no ‘r’. will you please provide any suggestions??

    thanks in advance!

  9. #9 by zedia.net - June 19th, 2009 at 11:02

    @sagar
    Well you could use my function but as an alphabet pass it something like “abcdefghijklmnopqstuvwxyzABCDEFGHIJKLMNOPQSTUVWXYZ” (alphabet but without the “r” or “R”) that should work.

(will not be published)
Subscribe to comments feed
  1. No trackbacks yet.