CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2002
    Location
    .NET 2.0/.NET 3.0/.NET 3.5 VS2005/VS2008
    Posts
    284

    Random Password Generator

    I have the following code for generating random passwords:

    Code:
    int RandomNumber(int Start, int End)
      {
        return (int)(Start + Math.random() * (End - Start));
      }
    
    String RandomPassword(String passwordSeed, int Length)
      {
        int i;
        String s;
        int x;
        String temp = "";
    
        for(i = 0; i < Length; i++)
        {
          x = RandomNumber(0,passwordSeed.length());
          s = passwordSeed.substring(x,1);
          temp += s;
        }
    
        return temp;
      }
    When I call the function RandomPassword it gives me StringIndexOutOfBoundsException
    What is the solution ? The index is generated correct (as far as I know). So there must be something else going wrong.
    WM.

    What about weapons of mass construction?

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    The second argument to String.substring(int beginIndex, int endIndex) is the end index of the substring - the substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Passing 1 for the end index means you're mostly asking for a substring of negative length...


    I'm not afraid of dying - I just don't want to be there when it happens...
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Dec 2002
    Posts
    11
    The best way to give would be
    s=passwordseed.substring(x, x+1)

  4. #4
    Join Date
    Jul 2002
    Location
    .NET 2.0/.NET 3.0/.NET 3.5 VS2005/VS2008
    Posts
    284
    LOL, Yeah that did the trick Thanks for all the help.

    P.S. Java isn't soo hard to learn
    WM.

    What about weapons of mass construction?

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured