1
2
3
4
5
6
7
8
9 package com.atlassian.core.util;
10
11 public class RandomGenerator
12 {
13
14
15
16 public static String randomPassword()
17 {
18 return randomString(6);
19 }
20
21
22
23
24 public static String randomString(int length)
25 {
26 return randomString(length, true);
27 }
28
29
30
31
32 public static String randomString(int length, boolean includeNumbers)
33 {
34 StringBuffer b = new StringBuffer(length);
35
36 for (int i = 0; i < length; i++)
37 {
38 if (includeNumbers)
39 b.append(randomCharacter());
40 else
41 b.append(randomAlpha());
42 }
43
44 return b.toString();
45 }
46
47
48
49
50
51
52 public static char randomCharacter()
53 {
54 int i = (int) (Math.random() * 3);
55 if (i < 2)
56 return randomAlpha();
57 else
58 return randomDigit();
59 }
60
61
62
63
64 public static char randomAlpha()
65 {
66 int i = (int) (Math.random() * 52);
67
68 if (i > 25)
69 return (char) (97 + i - 26);
70 else
71 return (char) (65 + i);
72 }
73
74
75
76
77 public static char randomDigit()
78 {
79 int i = (int) (Math.random() * 10);
80 return (char) (48 + i);
81 }
82 }