public class PasswordGen { public static void main (String[] args) { Generator(); } public static void Generator() { char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray(); int w = (int) ((Math.random() * 25 +1)); int x = (int) ((Math.random() * 25 +1)); int y = (int) ((Math.random() * 25 +1)); int z = (int) ((Math.random() * 25 +1)); char place1 = (alphabet[w]); char place2 = (alphabet[x]); char place3 = (alphabet[y]); char place4 = (alphabet[z]); char[] numgen = "0123456789".toCharArray(); int a = (int) ((Math.random() * 9 + 1)); int b = (int) ((Math.random() * 9 + 1)); int c = (int) ((Math.random() * 9 + 1)); int d = (int) ((Math.random() * 9 + 1)); char place5 = (numgen[a]); char place6 = (numgen[b]); char place7 = (numgen[c]); char place8 = (numgen[d]); char[] symgen = "!@£$%^&*_".toCharArray(); int sym1 = (int) ((Math.random() * 8 + 1)); int sym2 = (int) ((Math.random() * 8 + 1)); char placex = (symgen[sym1]); char placey = (symgen[sym2]); StringBuilder sb = new StringBuilder(); sb.append(placey); sb.append(place1); sb.append(place8); sb.append(place2); sb.append(place7); String passcode1 = sb.toString(); StringBuilder se = new StringBuilder(); se.append(place3); se.append(place6); se.append(placex); se.append(place4); se.append(place5); String passcode2 = se.toString(); if (Math.random() * 2 + 1 <= 2) { passcode1 = passcode1.toUpperCase(); } else { passcode2 = passcode2.toUpperCase(); } System.out.print(passcode1); System.out.print(passcode2); } }
Hi there,
I'm trying to make a random password generator, but it isn't as random as I'd like it to be. I have set places where either a number, letter or symbol can go. I'd like to randomise the placement of these set types in the output from "symbol + letter + number + letter etc" to "? + ? + ?". I hope that makes sense. Sample output from this code:
@W1L5m8%j4
^Y2L8z4@d1
£D6X5i3^i8