...

View Full Version : Random 4-Characters



CSS
07-19-2007, 07:50 PM
What I'm looking for is...
a script that randomly generates 100 4-letter 'words' that can consist of any amount of letters and numbers (1 letter, 3 numbers; 2 letters, 2 numbers, etc.), with only the exception that each 'word' begins with a letter. I'm newer to scripting, and I need this script rather urgently, and I honestly think I'm not understanding how to get this done. If someone could post this script and I'd be able to look at it, it'd do a lot for me.

Philip M
07-19-2007, 08:42 PM
Try this:-

<script type = "text/javascript">

var howMany = 100;
var howLong = 4;

function randStrings() {
var characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ"+
"abcdefghiklmnopqrstuvwxyz";
for (var i=0; i < howMany; i++) {
var word="";
while (word.length < howLong) {
var rand = Math.floor(Math.random() * characters.length);
addchar = characters.substring(rand,rand+1);
if (word.length == 0 && addchar >= 0 && addchar <=9) { // do nothing
}
else {word += addchar}
}
document.write(word + "<br>");
}
}

</script>


You can easily modify the script if you don't want the lower-case letters.

CSS
07-19-2007, 09:03 PM
Testing offline to no avail! Looking at the script, I can't really imagine what is wrong with it, it looks as if everything has been included. Then again, I'm not adept in JS... thanks for the help, I'm sure there is a very small problem with why it is not working!

I just tested it online as well, no-go!

coothead
07-19-2007, 09:44 PM
Hi there CSS,

I think that Philip M forgot to add the highlighted line to the script...

<script type = "text/javascript">

var howMany = 100;
var howLong = 4;

function randStrings() {
var characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ"+
"abcdefghiklmnopqrstuvwxyz";
for (var i=0; i < howMany; i++) {
var word="";
while (word.length < howLong) {
var rand = Math.floor(Math.random() * characters.length);
addchar = characters.substring(rand,rand+1);
if (word.length == 0 && addchar >= 0 && addchar <=9) { // do nothing
}
else {word += addchar}
}
document.write(word + "<br>");
}
}
window.onload=randStrings;
</script>
coothead



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum