FORUMS TUTORIALS MAGAZINE STOCKYARD VIDEOS PODCASTS EVENTS SERVICES NEWSLETTER NEWS BLOGS
 ADOBE AFTER EFFECTS: Home Forum Basics Expressions Tutorials Podcasts AE Techniques Training Creative Cloud Debate FAQ

# Random Letters

COW Forums : Adobe After Effects

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
 Random Letters by Ken on Apr 4, 2006 at 6:01:08 pm

Hi...Sort of a different take on a question I asked a few days ago that Mylennium was kind enough to answer. I have an expression that I'm using to generate a random 5 digit number like:

Math.round(random(11111,99999))

Works great, but now they would like to see how letters would look in the same situation. Is there an easy bit of code to modify this and make 5 letters happen?

Ken

 Re: Random Lettersby Mylenium on Apr 4, 2006 at 6:36:51 pm

You could try using the fromCharCode() or a custom array, the latter perhaps being more practical. Try this:

my_alphabet=["a","b","c",...,"z"] //insert the complete alphahabet

my_letter_a = my_alphabet[my_random_a]; //get the random letter from the alphabet

//create the same setup for all 5 letters

[my_letter_a + my_letter_b + my_letter_c + my_letter_d + my_letter_e] //print the result string

This should give you something usable if you apply it to the source text.

Mylenium

[Pour Myl

 Re: Random Lettersby Ken on Apr 4, 2006 at 6:45:04 pm

I'll give it a whirl and try not to mess it up.

THANKS AGAIN!

 Re: Random Lettersby Colin Braley on Apr 4, 2006 at 10:04:05 pm

While Mylenium's way will work fine, I did something similar to this recently and came up with a quicker way. Just use this expression for source text of a layer:

//Begin expression
numOfLetters = 10;//Modify me
useSpaces = true;//Modify me
changeEveryFrame = false;//Modify me

//Don't modify below this line

seedRandom(index, !changeEveryFrame)
//--
function genLetter( )
{
r = random(65, 90);//from a to z
return String.fromCharCode( r );
}
s = "";
for(i = 0; i < numOfLetters; i++)
{
s += genLetter();
if(useSpaces)
s += " ";
}
s
//End expression

Modify the first couple lines of code as needed.

~Colin

 Re: Random Lettersby Mylenium on Apr 5, 2006 at 5:18:06 am

Yeah, while it works perfectly, it's way to complicated for someone who's just started to begin using expressions. Not meaning to be negative, but there is no point in showing off your programming skills if the guy on the other end doesn't understand a thing and is unable to modify your stuff easily (Can you be sure he even understands the concept of ASCII/ Unicode char codes? Can you be sure he even understands the concept of functions, methods and variables?). You know, people don't learn anything from just copying pre-made code they don't see thru... In addition to that my concept can easily be expanded to include other letters or even entire words, which might be what he needs at some later point.

Mylenium

[Pour Myl

 Re: Random Lettersby yikesmikes on Apr 5, 2006 at 8:27:44 am

Hey, since Ken's head is spinning, why not throw this into the mix:

This is modified from Dan's "Exploring Expressions in After Effects 6", halfway down the page wth the grid of random numbers and text:

http://www.creativecow.net/articles/ebberts_dan/ae6_exp/index2.html

numRows = 1;
numChars = 5;
holdFrames = 5;

seed = Math.floor(time/(holdFrames*thisComp.frameDuration));
seedRandom(seed,true);

s = "";
j = 0;
while(j < numRows){
k = 0;
while (k < numChars){
c = Math.floor(random(65,91));
s += String.fromCharCode(c);
k += 1;
}
s += "r";
j += 1;
}
s

-------------------

With the text tool, click to get that little flashing cursor thing, then twirl down the text to reveal "Source Text" and apply this expression to the source text.

The way it's set up will give 1 row of 5 numbers that holds for 5 frames, change to suit yourself.

 Re: Random Lettersby Mylenium on Apr 5, 2006 at 8:57:23 am

Whoa, you guys are crazy ;o). If I asked my mom (she's a teacher) she'd probably say that this is against all rules of didactics and teaching methodology.

Mylenium

[Pour Myl

 Re: Random Lettersby yikesmikes on Apr 5, 2006 at 9:25:23 am

I just think it's nice to have choices, and on the page cited, Dan does a pretty nice job of, if not explaining his expression, at least shedding light on it. Anyway sometimes it's fun to have your head spin, like getting dizzy from twirling around and around.

 Re: Random Lettersby Colin Braley on Apr 5, 2006 at 3:34:38 pm

Mylenium,
I'm just trying to share what I know. While this expression may have been a bit complicated for a beginner to understand, I assume others who read this thread will be able to look through it and figure out the code and maybe learn something. And in Ken's case, he got a quick and easy solution even if he doesn't feel like reading through all the code and figuring it out.
~Colin

 Re: Random Lettersby Mylenium on Apr 5, 2006 at 6:10:58 pm

[Colin Braley] "Mylenium,
I'm just trying to share what I know. While this expression may have been a bit complicated for a beginner to understand, I assume others who read this thread will be able to look through it and figure out the code and maybe learn something. And in Ken's case, he got a quick and easy solution even if he doesn't feel like reading through all the code and figuring it out."

Please, no need to apologize. You guys are great (but still a bit crazy ;-D) it's just that my own rule of thumb is: keep it simple. Using loops, conditional statements and string methods certainly does not fall into that category. You know, the danger is that with such complex stuff you can easily put off people from ever using expressions again (out of frustration of not understanding the code)and that can't be what we want when we should be convincing them of the exact opposite.

Mylenium

[Pour Myl