Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random 4-Characters

    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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    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.

  • #3
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!
    Last edited by CSS; 07-19-2007 at 09:08 PM.

  • #4
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,718
    Thanks
    0
    Thanked 240 Times in 235 Posts
    Hi there CSS,

    I think that Philip M forgot to add the highlighted line to the script...
    Code:
    <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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •