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.

  10. #10 by Nehul - July 14th, 2009 at 19:01

    Thank you so much. I knew how to do it in java.

    anybody wants something similar in java here is the code

    =================
    String value = UUID.randomUUID().toString().replace(“-”,”").substring(0,5);
    int random = new java.util.Random().nextInt();
    String emailvercode = value+String.valueOf(random);
    ==================

  11. #11 by Dhaval - July 22nd, 2009 at 04:58

    Hi your function is really helpful but my requirement is little different i want a function to jumble the letters and return them back to me eg
    if i input “CLOUD” it should give me output as “CDOUL”

  12. #12 by zedia.net - July 22nd, 2009 at 10:38

    @Dhaval
    Basically what you would need to do is to put each character of your string in an array. After that all you do is swap random slots in your array (put slot 1 into 3 and 3 into 1 and repeat randomly a few time (30 or so depending on the number of characters of your starting string)) and then reassemble your String.

    Hope this help

  13. #13 by architortured - August 17th, 2009 at 01:11

    Hello,
    I also am very grateful for this; however, I have absolutely no idea how to implement it. Also, could you clarify your response to Dhaval? I am new to Flash, and wouldn’t be offended if you didn’t have time to explain this.

  14. #14 by Burton - August 27th, 2009 at 04:09

    Thanks so much for this. Quick fix for a need for some tokens for some game data!

  15. #15 by Arun - November 3rd, 2009 at 06:12

    Thanks . It was very useful !!!

  16. #16 by ChrisS - December 11th, 2009 at 17:19

    Thanks for posting this!

  17. #17 by Jk_ - January 4th, 2010 at 05:48

    Super helpful! Thanks for sharing.

  18. #18 by Molko - April 7th, 2010 at 07:04

    Thx for this, just what I was looking for !

  19. #19 by Ahsx - April 12th, 2010 at 16:06

    Hi, you don’t need to split the pattern to an array.
    He are my solution :

    public static function generate(length:int, pattern:String=”abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789″):String
    {
    var ret:String = “”;
    var n:int = 0;
    var mn:int = pattern.length;
    while(–length > -1)
    {
    n = Math.random()*mn;
    ret += pattern.charAt(n);
    }

    return ret;
    }

  20. #20 by vkwave - April 16th, 2010 at 07:18

    Thanks for sharing the code. I was looking for the same what you did.

    Thanks

  21. #21 by Jonny - May 15th, 2010 at 07:27

    Many thanks from Birmingham, England; this has saved me some time :o )

  22. #22 by Reny Mohan - July 27th, 2010 at 01:38

    Thanks alot..saved my time..

  23. #23 by gg - August 6th, 2010 at 03:32

    sorry i’m a very new flash user.

    on as2, do i create a dynamic textbox and name it randomLetter?

    on as3 where can i find the library for
    import net.zedia.utils.StringUtils; ?

    thanks

  24. #24 by Horoskop Waga - September 6th, 2010 at 20:27

    Good info

  25. #25 by Cdawg - March 3rd, 2011 at 08:21

    Added a little functionality to specify numbers or letters only.

    function gRS(newLength:uint = 1, userAlphabet:String = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789″):String{
    if(userAlphabet == ‘#’){
    userAlphabet=”0123456789″;
    }
    else if (userAlphabet == ‘!#’){ userAlphabet=”abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”;
    }
    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;
    }
    trace(gRS(150));
    trace(gRS(150, "#"));
    trace(gRS(150, "!#"));

  26. #26 by Sasha Dzeletovic - March 9th, 2011 at 22:33

    Thanks for the Class, nice of you to share.

  27. #27 by JennyArt - September 22nd, 2011 at 13:28

    Thanks, I need this to improve my AS3 skill.

  28. #28 by Carlos Zambrano - November 21st, 2011 at 20:13

    Great, but i dont know how to use it? how can made it working in a fla
    Tahnks

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