编写一个函数,用于生成随机密码,入参为随机密码的长度,出参为生成的随机密码,要求生成的随机密码必须同时包含大写字母小写字母数字。
V1(低效):
import java.util.Random;
public class Main {
public static void main(String[] args) {
while (true)
System.out.println(randomPW(6));
}
public static String randomPW(int pwLen) {
// 长度至少为3
if (pwLen < 3) return null;
char[] cArr = new char[pwLen];
// 随机设置数字字符数量
int numLen = new Random().nextInt(pwLen - 2) + 1;
// 随机设置大写字母字符数量
int upperCaseLetterLen = new Random().nextInt(pwLen - numLen - 1) + 1;
// 设置小写字母字符数量
int lowerCaseLetterLen = pwLen - numLen - upperCaseLetterLen;
for (int i = 0; i < numLen; i++) {
int randomPos;
// 算法瓶颈在于这个while循环可能要花费较长的时间找到一个合适位置
while (cArr[randomPos = new Random().nextInt(pwLen)] != '\0') {
;
}
cArr[randomPos] = (char) ('0' + new Random().nextInt(10));
}
for (int i = 0; i < upperCaseLetterLen; i++) {
int randomPos;
// 算法瓶颈在于这个while循环可能要花费较长的时间找到一个合适位置
while (cArr[randomPos = new Random().nextInt(pwLen)] != '\0') {
;
}
cArr[randomPos] = (char) ('A' + new Random().nextInt(26));
}
for (int i = 0; i < lowerCaseLetterLen; i++) {
int randomPos;
// 算法瓶颈在于这个while循环可能要花费较长的时间找到一个合适位置
while (cArr[randomPos = new Random().nextInt(pwLen)] != '\0') {
;
}
cArr[randomPos] = (char) ('a' + new Random().nextInt(26));
}
return String.valueOf(cArr);
}
}
V2(高效):
import java.util.Random;
public class Main {
public static void main(String[] args) {
while (true) {
System.out.println(randomPW(6));
}
}
public static String randomPW(int pwLen) {
// 长度至少为3
if (pwLen < 3) return null;
char[] cArr = new char[pwLen];
// 随机设置数字字符数量
int numLen = new Random().nextInt(pwLen - 2) + 1;
// 随机设置大写字母字符数量
int upperCaseLetterLen = new Random().nextInt(pwLen - numLen - 1) + 1;
// 设置小写字母字符数量
int lowerCaseLetterLen = pwLen - numLen - upperCaseLetterLen;
int[] randomPos = getRandomSequence(pwLen);
// 依照生成的随机序列散列三种字符
for (int i = 0; i < pwLen; i++) {
if (i < numLen)
cArr[randomPos[i]] = (char) ('0' + new Random().nextInt(10));
else if (i < numLen + upperCaseLetterLen)
cArr[randomPos[i]] = (char) ('A' + new Random().nextInt(26));
else if (i < numLen + upperCaseLetterLen + lowerCaseLetterLen)
cArr[randomPos[i]] = (char) ('a' + new Random().nextInt(26));
}
return String.valueOf(cArr);
}
// 高效的随机序列生成算法(0 ~ pwLen)
public static int[] getRandomSequence(int pwLen) {
int[] input = new int[pwLen];
int[] output = new int[pwLen];
for (int i = 0; i < pwLen; i++) {
input[i] = i;
}
int end = pwLen;
Random random = new Random();
for (int i = 0; i < pwLen; i++) {
int temp = random.nextInt(end);
output[i] = input[temp];
input[temp] = input[end - 1];
end--;
}
return output;
}
}
1s0ywS zrD912 0Bmgaf LM3Vxe D3I3My 0pFxrz 1tlwrJ A1Ae68 Le9151 403dL4
48d2UM dgF7i0 i2H335 oZ1PNO 9t28P2 6e0UGd n61e0W auA4LM 2741wX Vf44VF
4Om5os 5N389c BaQM5R ze2Jpe FWVu3R XQdV91 zx78Nx dONM4P zHi1ly aS0tm8
2G7f01 MJc0k4 I883pH oe40RK fY0gVn 2h98YQ h2S560 qoGhj6 nOKh68 QXyS8P
AMY3g2 OQuY05 a0482G Z8OMy3 9QbG55 u6i8I7 R8qRzu Uts7hS jA0L0P mm97Pk
lv7M4z 267Qi0 yPa80f 4g4vzM g8TB7Q FY3ZeW 3yRs90 B34k94 Bvl6vp 1rzkiA
kAbn2k b4KRxd Dm6BnE 7hRJc2 4768Zr b361wN 12faWn 6807Wb 5Pxs19 k7241B
f254Lo 5i4D08 1kX71n 05bHX7 o6Q9HT v3Q608 43F04m 0mj27C 4T192d 6Es1X6
8CJNws 2Yp06I 9Yok95 6j67gT r1tB1y 641h4E Z36bf6 3jr9hG 3dJ54x jFf0vs
50gXZ7 j3249B M7U33l y99T9n 4XOVv8 vB1A27 6aK835 0IA98g 0psF9k oAKE36
u7bfqH HvJZ5x ow1fQ4 5y6O7p 46V78l 08E1vM PV6JOz w3cNZg 2oV59j aA15fb
Pk04Lb 1o25R7 5fJs8q JNx59g 9fqvcA f68Iz8 nMz20u yCN7lD uZSm4H n4918Y
38Fz3W 5TVV2n Jn508P Y4fwof 2188fU QXXx67 Rj9IC4 0LPM7v K3uT7D x26l5V
8W71a6 cBK1Va ZD4BjI IcfSz2 8088Jx N801q0 AcID06 F1Qfx5 65bTll haIu5D
e5iR5M YkjY6C 85Xkh3 46Q3gp s4X828 06Ks4m 750H4z 438vH1 pwQ07j PfNSQ6
c1oNkS sgd26L 1sB31s n1g0D3 OY9cU1 b7oW5t x446Ck Ss6Nzu g46I43 oOQ42E
3Z44j1 Ol6bou qR4812 p2wVt5 7cKeac z4c18P KL0NnF sPe9ce 2D8v49 vOwl48
F2k335 x0rIP3 ryXrn7 7yK80O e4Pkes 9054kP j505D7 1q7H79 2RNlet ay25Ku
0G19Uv t8PXc3 6ZfIdp 84Yhyh Cu8173 f3KEhi H3urff 2S4q87 o4OsE9 A04iJo
Jo4rWI cV1qWB eEsC7s 7c34aT 8Sm8YN s93Yz6 pX0p6e IH7y3Q 1e6Y25 865rP6
4H3ob7 BgKo6a i4qP4E v8x7bB Ey89Yo Re7427 5AZ26h oU0ko0 b6E3vG Hj9H6o
2y478D 9Td122 eJ4756 mu0ubP H4zK0Z oRZ25z VmUk1y A8KHJq K93j59 mil8Pz
RPif70 2xKFTB 3W56z0 8mK411 93Hds6 0519Bk if97RL 645e6S 712BxZ 1i53Z2
05k0O6 cWM9K3 8It71v 2y1A83 y98yXu 42Im1H p2tHUR V5AEs8 mL97Gc 5i2WJ7
6MUiHY e08Az6 KVB0w4 j7GdOq 953Uz7 8Hs740 sP2LVy sgGwc1 Z2DgjO lA8VOG
Y1FLTw 4LTy52 7QJ8kg z9X90S ZB4brh XL4s53 6Bq416 6dpQ4a 4C0c15 8l6Br4
u5U873 6n74F7 los4Dh dl94GS 5p4BsB 556hI9 h7F302 A83w8K Z9246k 3Wg5u5
1oAy47 0sWaie rhNJA1 A4i63E 66eBwu C84B0t 7GwOON f31U1V Im1649 So2uo7
r2B829 g3F9o5 DX9h8A 631zhC U6uToJ 4nZf1Q w3v1Xp rdtw3N h404L6 P941m8
6FcU2m Z88Q8h 62wF59 2KfSnd G35h55 NCgAE7 Zd58pe W8O1nw K673Me r9KOi8
3ZAeq6 4bjb6W 1R3cUR 7SX4Zm lRZ2Wn 9VX41l ex6HtA 1nRgQ7 71d42T 1Rat6u
v639zF yLtMq2 20lp0K 97H3jq d305BQ eL0kuH kc0a4E 2v26iL 6eiJ7D 8T2pr7
85Ls7L cwV27V FV6r53 LydI5o 4o580P 4w960M k4tuJ9 b7USEE 9S28z1 4145Cv
Pkh7p8 61P9i2 zJF3oF i5SiNy 7t0S23 2A9n13 3CiW7s 8E4GAj bG300p T682l4
E888d9 kz4Vkf n2puVa Fa4swI InDB3K 4fGjSM F6aFFv 1iJyT2 BjguI0 VHn84s
s0G353 uY2eea vrQ63E AIw4AA f3zTq8 nbG3x3 362gK3 ps2dF3 a9J30S 27FCCd
dYaE8k i2007W P5801p 60VgJ3 71guEr 61sGxv ds8X5M fH2T72 2f6F78 A3npyv
kBQ2e7 dOdq9n 9fV4XZ 7z1hLu 1623La U6TsCj FT5DAp 9Nr1K3 n5xwmL 4A9Mt5
748rGN Q9h4M6 t74Q12 6qmG1b 9V16n0 j0Q325 R0yAqZ 95c8Mx 4xN4K8 1n035T
Hv5rSc 1T0n52 ylw8hN 736t6K g8bPej gdCs7d 03Lyz8 W383w0 P2196c L5plxs
gpuKA5 6dVm5e Wz6jJ0 3sFuj1 uNA0SX ZW23hP 1I7wO2 2138dJ 758p9I 73G11n
bI52c7 74Dz68 T99U3o fkK8nt b6mTMW C690n5 amF4z1 pCc9Zi 1Dn143 7W927r
9bv7CK 239Aw2 slJ5lq 8igUBX 21H7qY cWpm34 Y8HA4j 2zu9Ww l3BL58 32j99G
690Vw8 n2xxJ2 oOW6BH 77fA47 R43Kb4 7q730Z 1usz0T 498PIj Y1q348 56G67t
e2438O Ujs36E kP6s30 68exG7 2kVsI4 fL6cv1 Qa8501 uQH113 r9D4pv r967hT
D09i64 RVp0QU 9cZ0IY aN4985 Yt67y7 fC7Y28 L9pb5c I519b2 15i4Q6 DUK9hi
8KIxFx yo41uQ 63hWEI 78lXqg Hb901V 51cM05 05dVqv F317z8 lZ5767 c5vArs
285Dc7 0Z73mq y275ML 7188gV 428g0X fP4RYD zEu5Ba c49A7f 1f27bM s15aXy
57Zi19 2ka3Uo 9qX99K O57bV9 8S5f8j 7N48aN 29Fkz8 6b8k8R kgl0Ks V3mypq
g6d4GG 9vH30c o0PgAJ 0BQo5c 7xvtRs lvJ6vK Uzi9pR z4X896 pHnYk9 8035Ye
B8CYf1 9wxtgI 93blCm fXi8Of H4Ro0B 1g3Y0b Y5z1Pl iQ5GCC Wm9vqX 0eTZHH
o0YKO9 SepN2v b8W72f hTdjo3 28gxU3 A39j7s RO6l85 Z5y194 ofvm5Y wsfb2D
6H4e53 1z811B Sj6jKZ Tlc1xg 46nbdH 6R9nk3 4s545C 42rEdc 8995Pd 801j0A
e83I09 ar3J2S Z951r6 usC2Y3 OT2ufx EC89ad k3d7GG 72q4O5 mY3d1r 4ar6ML
2M576s f2jTUz 0G2Gcq tjL4oA d75U20 70Lf14 ALB3fz 56K52x Cemj5q Z433Yb
0j938V V8UOw4 UnmRE9 83Z16d 97Ekgy p3uTlw IBg106 ys8n7P DpBXf9 qwLf0s
1E57u4 39jnO2 vQm6u8 96ppMH NxHqm0 a45D2R 03n1M9 w2V8O5 k25W1p TZ302e
pW1tn4 fqxQ71 C5a73S 469fRk GU8HTc 89eH67 7VaEbA zz019M V8g9Sy O1qZrd
rV5CzX z1w5Q2 g4037A aW7Ovs V8k5C1 32g10K S8d5WH 1F060b 8U85a6 4YT9m0
eB9Pt2 nn3iQe edO5c4 4cUD22 8F7w39 iC5ix9 8v9dMt 78P22b 28Oe57 eBTC28
Jd5921 V35s0U Df95sK dZ49G9 CP8Nx1 51i3B1 BSjo7h 6KJRcE E9C7pN 8lTkxb
p0F1Uf 200c2O 7p238W p9ELhm t8TeBp U44q19 N846bq X1hp67 e1e1nA w3Sn9v
DcSB1W UhH2CN 23qtQv k81yiO 3f61dW 53r4NC b8M69N 4qA7M9 a9D670 R859i5
W604q1 c12L94 k391Y3 Q5fpal HBn85B D4jEFu d6A4pn 9Pu653 Pwfes3 d2Uzlb
lkK7sL j1519M fQ7y42 K65wzw d087Lb 6K6g1i N4k1dq 670Ad0 h6v8TY 6R63q7
o8KLn0 06RF5l 5I900c 95SfEx 3Zy262 0rrVh4 yc3KlO 0G7OFl 04b02B lNj24p
3P14i7 v1q96A wr3dnK bF6QZ1 8p8Ms4 73P8r5 rAg868 z03V2y D9UGtJ y2146I
G7ubI6 0NG3b8 V3VjpD V3l50m MDt5bN A46UMh NcF2LC qU0Pnh H5zrvf 983u0Q
1ml33Q 4Itq0L u335D0 U6QbQG 0oT5XE 9sEHOq 5KEXoF P69aES 07Hh72 44RUQl
2rK3bn q0A5BV R04u5W ue9GMj gGND9S 7Q523e 6nulX8 2ZeWr6 Pk8001 PV9ncQ
prU4Lq Gn3717 1jG3Z8 T99g46 29Wc94 aqK9WZ uX6zzc 1z7U32 60jB00 0u30AA
87MgbN JyN1Yn r13k2A 77Z41v tVm3De U85S3i N7k344 S1Ouly 5Za7s6 AAk6Gp
3BBtWQ 544ATf 3tLX28 14X65a KtA60C 771S2d 8368On zf4d2Q aE1703 ueCan6
F4M0jv Am37l7 6g8O29 x654K7 Ed4378 n1V554 k57F78 5mUz79 F2i2Wd 0Py719
8s2BPS 3jaH6O dZj233 z8413B 5y2R0y u33YuH V5IHvo To9dBG 9C9jP7 bE75Yx
L9x579 n8iAOH h69M9H hjT037 K459m9 kpL68u 5671Nh l332N4 e8JFQ1 BhZ0YV
Pk432y OXNN8l 31O0yT iccR6R V2KZTt 6hwUIh 464Vk8 6Ts6e9 02u86Q KwIj8M
52tApb dTaz5m 7yLkTh 7vY282 Dz32H6 92gklO 6Z5Kf7 06aCQ0 oBI04O 1d4Gsg
3E6b87 3D775v B1U8v7 91s62V Xh6ouj aiQ340 31b3A1 lOuN44 n211K5 FK1i50
VWFQp6 pu84I4 7O5h6O AOFiP8 42Xxu7 t8256D iID0al gF38EQ 5bkqeH 00oGh5
9lBfpQ s0340X v1371Y Q7jxaQ x43xT3 vygs3W g4gmXe 55Vu16 v7YX81 ZfQJV3
514UuU 94nIB7 FQ2n9w L4qoiz pZn8W7 n3ClY2 53U45s EmG7WG 189kG5 D27Kay
8aPe55 Z0fJJV j7eTgp Q45eU6 0yTD70 ikP0E9 QJWx8l P7BAuX c3685V w3avFK
U8bAAW DqP2Od 832J8l 4GOsBI k3J3fy BkRn2M n9Khak 4Fss97 p40Z1c R8Y0Qh
9Rq768 1xp6gF 0592Nh 05zJ8T 9L5rCt 3f7M13 163eTs cQ6FGZ xHH56m HGwPH8
7zG6w6 Zqi6CE 6990Us Grm470 t80KYB hQoik4 1kR917 vGm527 GRkFt5 i7xzVT
Snb5Ep 1Nl7m5 Suo26d E5kf0k U015n2 M0juQg 14v03Y z725H8 A41bd1 6X6ryV
99sISm AVfQ4D Y3PnIM o4Bc38 eAj1p9 2zpAHt O94t5N 0q286M 57O74f 3alX51
oQNX3d ru2L87 x8799L JdNxn3 4Y2k07 6g96Rl 6X123f i0r4MX D9v1iM SGfBF2
1I94l6 5J269w x6YRyV dDY9S1 o7E9sp Ia63JF IaNPa3 6oU1m2 6751yP r6EkFW
WKff6b i5Fq26 4r6B0k W3CuLP w1fpW9 1K9TRd o9H87c ri3mjS 3In8ZZ WL86c1
A1ZOwr OCqhg9 Eu9e4a sHm66X 911y6S 5Sa05d 48T35g I5Rjox G2fXnd 0j796W
A84S4o g34WzD 4Yy91L 5T5r10 0039Oh f5SU9G 8z3xmE s4n3K8 cGk41S 52u1R9
040s9A ndE2xe lB1ozh v7jO1o P7Hf3x 3186Ei t5T117 z4S370 7943pR N2fS4S
157P9p mm8P3W yEXg3M DK6cb1 u35evA xeR6pj 4nAuYM 1tRCTp 8k53S2 m8D485
V7RiN9 9dH221 P0Ksv0 L40bwr tQs630 31mW4W T71Px8 s3KVP2 aV103Z 4983sG
e83IJJ gP1394 Enb8Xv H06luG Cd97tq J5642e il5JM6 2sfI49 KXc1xK XFU9iY
sV2uWq wG88e5 K841a8 3A8g48 t8D38n oR4LWZ h3K3vs W297j1 6g77Rb Zab9wa
4r3U68 1pEw4f J1d941 T9xpmT 2P12tH 95Ms21 y6Nl8U S1f1K4 5dZ424 5P9rhe
WhwMe5 Dbda9x J7003m 8ySQ5H 1loVMS hF16Ge 3sL8NR ldysZ7 2R7LCc N966y0
N2L6Wv 3C12l8 Mo0FGR 7J2tU6 a9Vd9S EFrWQ4 f6Tgxf 94kaM9 Dqsyj2 VyUf0S
vmW378 tjS583 0tkVjo 1P8oj4 6P660g 5emrU1 514h9A HtJ6mb H6b3U4 77F4vn
t9B81P e6B452 lYJ2Py c3tr8Q QzSX2J dY8v0O p977P3 85ccJ1 4B7s15 9L2g14
474woP 9IgwQr 9NRll7 C86vvm 07I31c NM688b 77gMNE fHX433 ctA346 o656W6
49kyVH K5sbDu 0Q4qc4 2O9c14 dgZ3V9 xGY534 Vs7ba4 JD5f6l 3j75A7 f0Z8J9
D0h77d 9Eev4J ek14JQ Ib61mq qYd317 495J6c aN7Cz6 PkaO3G Aa9L72 511wR4
qK12v0 o34WK0 Sgw4E9 h98J93 X46h99 z27P88 883Xw3 f6B308 M6MJsQ V842dH
E2y429 v5yTz2 pe8Mc9 12N7qQ gwU396 4rfHM1 n649EB 825AlW ierE82 13cBtg
9518tM g3Sgfp A81e09 pnhSV6 dF92Xz 6O7r35 ZS2t6U P8v4R2 5kJ970 o322Z0
nIxV2j 8sUjWY JnU2PJ n8rJz2 374Pod A30K3h PMBb4R k6wV89 66Qz0G UzL2Bl
pJ8390 U52DuP 39zJ77 7t55K5 7a3U66 W4637s pTW89D e4126V 5u64P1 2P9A2v
AYE91k 646Zuv K0Jl61 ukG53X 2cxBh6 64e75H PQx3FL i09yBU TjSD32 h19U47
LFvI8T 9Ggkum 187Lk7 3n1E13 luYLO7 3I2XjH 12pN57 w18EsS CI45w7 2dUxsv
047d1H rEx8us Tz4K7H 84n1fD Sj0yAZ JJ86yp 4hQ487 I46vI5 182yEK 1T4yme
c6ZpO3 w5Ikk9 x6b2U6 m7G20M 9Wb689 35E78h 8dW818 48dd4W 38UDlf 7G8n24
H1tE1F 64Q9c2 ZFWVw7 2mPgqB a79tnX 47At96 4RJUXj ae9fLk 4w5C32 56J44b
13pXyq W7832q 4YiTKh 9wTtML rUwuf4 63Q12q fNA7Zz Ea9rU3 4B707i E7091g
hI0f46 S88GRt C9627d 1989Gd 28L59z 2J5e31 7a03nJ 1zvaSL x962V7 Vat5r6
AR8f81 dDmw3k hV3n96 zeq2K2 1952Kl 85Q17c ZJCo60 HE1sIq Hr61v5 EtIV4G
S40D3t 4NM4s9 l967G1 473KzQ 5u5RbC Me356X mGQE29 CYrTn8 3mpIg8 7P5c71