<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>zedia flash blog &#187; Strings</title>
	<atom:link href="http://www.zedia.net/tag/strings/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zedia.net</link>
	<description>Flash, ActionScript, SEO and everything in between</description>
	<lastBuildDate>Wed, 21 Jul 2010 17:07:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>generate random Strings in AS2 or AS3</title>
		<link>http://www.zedia.net/2008/generate-random-strings-in-as2-or-as3/</link>
		<comments>http://www.zedia.net/2008/generate-random-strings-in-as2-or-as3/#comments</comments>
		<pubDate>Sat, 10 May 2008 23:03:57 +0000</pubDate>
		<dc:creator>zedia.net</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[ActionScript 2]]></category>
		<category><![CDATA[random Strings]]></category>
		<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://www.zedia.net/2008/generate-random-strings-in-as2-or-as3/</guid>
		<description><![CDATA[I was searching for a function to generate random Strings either in AS2 or in AS3 but I couldn&#8217;t find any so I made my own using code from a typewriter effect, but I can&#8217;t seem to find the page anymore. I use this code when I load dynamic content from php and such and [...]]]></description>
			<content:encoded><![CDATA[<p>I was searching for a function to generate random Strings either in AS2 or in AS3 but I couldn&#8217;t find any so I made my own using code from a typewriter effect, but I can&#8217;t seem to find the page anymore. I use this code when I load dynamic content from php and such and I don&#8217;t want flash to cache my request. Here is an ActionScript 2 version of the code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> generateRandomString<span style="color: #66cc66;">&#40;</span>newLength:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> a:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&quot;</span>;
  <span style="color: #000000; font-weight: bold;">var</span> alphabet:<span style="color: #0066CC;">Array</span> = a.<span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">var</span> randomLetter:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
  <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> newLength; i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    randomLetter += alphabet<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">floor</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> alphabet.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>;
  <span style="color: #66cc66;">&#125;</span>
  <span style="color: #b1b100;">return</span> randomLetter;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>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:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;">package net.<span style="color: #006600;">zedia</span>.<span style="color: #006600;">utils</span><span style="color: #66cc66;">&#123;</span>
  <span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">class</span> StringUtils<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> generateRandomString<span style="color: #66cc66;">&#40;</span>newLength:uint = <span style="color: #cc66cc;">1</span>, userAlphabet:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&quot;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">var</span> alphabet:<span style="color: #0066CC;">Array</span> = userAlphabet.<span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #000000; font-weight: bold;">var</span> alphabetLength:<span style="color: #0066CC;">int</span> = alphabet.<span style="color: #0066CC;">length</span>;
      <span style="color: #000000; font-weight: bold;">var</span> randomLetters:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
      <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:uint = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> newLength; i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
        randomLetters += alphabet<span style="color: #66cc66;">&#91;</span><span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">floor</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> alphabetLength<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>;
      <span style="color: #66cc66;">&#125;</span>
      <span style="color: #b1b100;">return</span> randomLetters;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>here is how you use it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> net.<span style="color: #006600;">zedia</span>.<span style="color: #006600;">utils</span>.<span style="color: #006600;">StringUtils</span>;
&nbsp;
<span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span>StringUtils.<span style="color: #006600;">generateRandomString</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//for a random string of 4 characters</span>
<span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span>StringUtils.<span style="color: #006600;">generateRandomString</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span>, <span style="color: #ff0000;">&quot;ROGER&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//for a random string of 4 characters using only the following letters: ROGER</span></pre></td></tr></table></div>

<p><a href="http://www.zedia.net/fr/2008/generer-une-chaine-de-caracteres-string-aux-hazard-en-as2-et-as3/" title="Générer une chaîne de caractères (String) aux hazard en AS2 et AS3">Cet article en Français</a></p>
<div id="facebook_like"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.zedia.net%2F2008%2Fgenerate-random-strings-in-as2-or-as3%2F&amp;layout=standard&amp;show-faces=false&amp;width=450&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:550px; height:70px;"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.zedia.net/2008/generate-random-strings-in-as2-or-as3/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>
