Avatar ·

How to generate a random alphanumeric string in Java?

Hello everyone! I need help generating a random string containing only digits and letters (Latin, case-insensitive). I'm writing a small application for generating passwords and confirmation codes. I tried using `Math.random()` and character concatenation, but the code turns out terrible and unreliable. Here's what I have: ```java public static String generateRandomString(int length) { String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { int index = (int) (Math.random() * chars.length()); sb.append(chars.charAt(index)); } return sb.toString(); } ``` This works, but I'm concerned about using `Math.random()` — I've heard it's not the most secure