Program in JAVA to generate random alphanumeric string This code will generate a pseudorandom alphanumeric string of given length.
I would welcome suggestions on how to make it more random. Beyond that, have I made any convention violations, Exception cases, and the like?
Also, is there any way to make it faster?

public class Test
{
    public static String getRandomAlphaNum(int length)
    {
        String charstring = "abcdefghijklmnopqrstuvwxyz0123456789";
        String randalphanum = "";
        double randroll;
        char randchar;
        for
        (double i = 0; i &lt; length; i++)
        {
            randroll = Math.random();
            randchar = '@';
            for
            (int j = 1; j &lt;= 36; j++)
            {
                if
                (randroll &lt;= (1.0 / 36.0 * j))
                {
                    randchar = charstring.charAt(j - 1);
                    break;
                }
            }
            randalphanum += randchar;
        }
        return randalphanum;
    }
}
for
        (double i = 0; i &lt; length; i++)


And related loops should have the "for" on the same line (as this is a common coding convention.)

(randroll &lt;= (1.0 / 36.0 * j))


This doesn't have to be a double; instead, the random number can be generated as an integer (to select which element from the array.)

randchar = '@';


Unless the random string is not random, I would not initialize the variables with sample data. I'd just leave them blank and then adjust the loop to always run at least once (a do-while loop) so that it becomes initialized.

for
            (int j = 1; j &lt;= 36; j++)
            {
                if
                (randroll &lt;= (1.0 / 36.0 * j))
                {
                    randchar = charstring.charAt(j - 1);
                    break;
                }
            }


I would remove the inner if-statement and un-hardcode the values so it can work with strings with any size. Applying these suggestions, it can be simplified to:

import java.util.Random;
class Main {
  public static void main(String[] args) {
    int strLen = 100;
    String randString = "";
    Random r = new Random();
    String[] chars = "abcdefghijklmnopqrstuvwxyz0123456789".split("");
    while (randString.length() &lt; strLen)
        randString += chars[randBetween(r, 0, chars.length - 1)];

    System.out.println(randString);
  }

  /*
  Generates a random number from min to max inclusive
  */
  public static int randBetween(Random r, int min, int max) {
    return r.nextInt((max - min) + 1) + min;
  }
}


This approach is not optimal as the string is constantly being appended to, meaning that the string has to be re-copied every iteration.

Java introduced Streams, which allows reading forever from certain generators. Knowing this, we can read a stream of random numbers up until the string length that the user wants, and then get the character at the random string length:

import java.util.Random;
class Main {
  public static void main(String[] args) {
    int strLen = 100;
    String chars = "abcdefghijklmnopqrstuvwxyz0123456789";

    StringBuilder randomOutput = new StringBuilder();
    new Random().ints(strLen, 0, chars.length())
                .forEach(c -&gt; randomOutput.append(chars.charAt(c)));

    System.out.println(randomOutput);
  }
}


StringBuilder is used to append the random character as it doesn't have to be re-copied for every loop iteration.