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 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2002
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help with random letter generator

    Hi,

    script below works fine for numbers but getting it to generate a random 4 or 5 letter is a nightmare. Where is it going wrong.

    thanks in advance

    D

    <script type="text/javascript">
    <!--
    var randomnumber = Math.round(Math.random()*1007700);

    var randomletter = "abcdefghiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXTZ";
    var randomalpha = Math.floor(Math.random() * randomletter.length);

    document.write('<a href="http://www.site.com/redirect.php?id='+randomalpha+''+randomnumber+'">Link</a>');
    document.write('"'+randomnumber+'"');
    document.write('"'+randomalpha+'"');

    //-->

    </script>

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Firstly;
    randomletter is one long string not an array so you will need to use:
    charAt(randomnumber) or indexOf(randomnumber)

    Secondly;
    Is this a homework assignment?

    .....Willy

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    This will give you one random letter. If wanted to get more using this method you would have to loop it:
    PHP Code:
    var randomletter='abcdefg...ABCDEFG...'.split("");
    var 
    randomalpha=randomletter[Math.floor(Math.random() * randomletter.length)]; 
    I shortened your randomletter string for brevity but you should get the idea.


  •  

    Posting Permissions

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