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 |



#1 by Daniel - June 10th, 2008 at 13:59
Hey, this is super helpful, thanks
#2 by David Patrick - June 13th, 2008 at 17:48
Thank you sooo much, you’re the best !!!!!
#3 by Michiel van der Ros - October 20th, 2008 at 11:02
Ah, this saves me some work. Thanks for putting it online!
#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 by Michal K - March 2nd, 2009 at 01:48
Awesome, totally gonna use it for my project. Thanks!
#6 by free_lady - April 16th, 2009 at 03:56
thanks for this
#7 by chris - June 18th, 2009 at 04:59
It works like a charm.
Thanks
#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 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 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 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 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 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 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 by Arun - November 3rd, 2009 at 06:12
Thanks . It was very useful !!!
#16 by ChrisS - December 11th, 2009 at 17:19
Thanks for posting this!
#17 by Jk_ - January 4th, 2010 at 05:48
Super helpful! Thanks for sharing.