Random Password Generator
Mathematica (18)
Let me a little cheat
= 15char ASCII pwd
&(^F7yP8k:*1P<t
P.S. not safety :)
Ruby, 74 69 bytes
Just randomly sample from the ascii range 33 - 126 until all classes of characters are present:
$_=[*?!..?~].sample(15)*''until~/\d/&&~/[a-z]/&&~/[A-Z]/&&~/\W|_/
p$_
Ruby, 39 bytes
Using moose's clever discovery:
p"0123abcdABCD-+/<".chars.sample(15)*''
Edit to satisfy the mob:
Note that the rules changed after I first posted this. At the time both the previous entries applied to the rules. I would also like to point out that the rules are still not too well defined:
(..) all permutations of all allowable characters
"Permutations". There are no permutations of the allowable characters that complies with the rest of the rules, because any permutation of the set of allowable characters is as long as the set of allowable characters itself (while the password is supposed to be 15 characters long). And there are no repetitions in a permutation. However my first entry is still more "random" than many of the other well upvoted answers here.
Nevertheless, here you have it. Allows repetitions of characters and underscore:
Ruby, 77 bytes
$_=([*?!..?~]*15).sample(15)*''until~/\d/&&~/[a-z]/&&~/[A-Z]/&&~/\W|_/
puts$_
I also used puts
instead of p
in this one because p
prints out the string enclosed in "quotation marks" and some characters escaped with a backslash.
Ruby, 70 bytes
As Ventero points out, ~
can be skipped in front of the regexes, and print
can replace puts$_
. But with the ugly output this causes you might as well print all the rejected passwords too, squeezing it into a one-liner:
puts$_=([*?!..?~]*15).sample(15)*''until/\d/&&/[a-z]/&&/[A-Z]/&&/\W|_/
Explanation
As requested. $_
is a semi-magical variable that contains the last line read from input - so you don't always need to store it, like this. Here however we use it because of another property, namely that the ~
operator applies a regex directly to it, a trick I first learned by chron. I replaced the usage of all
, but it should be quite easy to understand if you get the rest (see the docs).
Java 8 - 354 329 319 275 267 characters
Just for fun, using lambdas with Java 8 - each possible output has the same probability of being found.
It uses the fact that the allowed characters have consecutive ascii codes, from 33 to 126.
class A {
//flags for, respectively, small caps, large caps, digits, punctuation
static int a, A, d, p;
public static void main(String[] x) {
String s;
do {
//Using special String constructor that takes an int[]
s = new String(new java.util.Random().ints(15, 33, 127)
.toArray(),
0, 15);
a = A = d = p = 0;
s.chars()
.map(c ->
c > 96 & c < 123 ? a = 1
: c > 64 & c < 90 ? A = 1
: c > 47 & c < 58 ? d = 1
: (p = 1))
.min();
} while (a + A + d + p < 4);
System.out.println(s);
}
}
Sample output:
.*;Tm?svthiEK`3
o.dzMgtW5|Q?ATo
FUmVsu<4JF4eB]1
Compressed program:
class A{static int a,A,d,p;public static void main(String[]x){String s;do{s=new String(new java.util.Random().ints(15,33,127).toArray(),0,15);a=A=d=p=0;s.chars().map(c->c>96&c<123?a=1:c>64&c<90?A=1:c>47&c<58?d=1:(p=1)).min();}while(a+A+d+p<4);System.out.println(s);}}