|
@@ -3,6 +3,12 @@
|
3
|
3
|
*/
|
4
|
4
|
var ALPHANUM = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
5
|
5
|
|
|
6
|
+/**
|
|
7
|
+ * Hexadecimal digits.
|
|
8
|
+ * @const
|
|
9
|
+ */
|
|
10
|
+var HEX_DIGITS = '0123456789abcdef';
|
|
11
|
+
|
6
|
12
|
/**
|
7
|
13
|
* Generates random int within the range [min, max]
|
8
|
14
|
* @param min the minimum value for the generated number
|
|
@@ -37,38 +43,27 @@ function randomAlphanumStr(length) {
|
37
|
43
|
return result;
|
38
|
44
|
}
|
39
|
45
|
|
40
|
|
-/**
|
41
|
|
- * Generates random hex number within the range [min, max]
|
42
|
|
- * @param min the minimum value for the generated number
|
43
|
|
- * @param max the maximum value for the generated number
|
44
|
|
- * @returns random hex number
|
45
|
|
- */
|
46
|
|
-function rangeRandomHex(min, max)
|
47
|
|
-{
|
48
|
|
- return randomInt(min, max).toString(16);
|
49
|
|
-}
|
50
|
|
-
|
51
|
46
|
/**
|
52
|
47
|
* Exported interface.
|
53
|
48
|
*/
|
54
|
49
|
var RandomUtil = {
|
55
|
50
|
/**
|
56
|
|
- * Generates hex number with length 4
|
57
|
|
- */
|
58
|
|
- random4digitsHex: function () {
|
59
|
|
- return rangeRandomHex(0x1000, 0xFFFF);
|
60
|
|
- },
|
61
|
|
- /**
|
62
|
|
- * Generates hex number with length 8
|
|
51
|
+ * Returns a random hex digit.
|
|
52
|
+ * @returns {*}
|
63
|
53
|
*/
|
64
|
|
- random8digitsHex: function () {
|
65
|
|
- return rangeRandomHex(0x10000000, 0xFFFFFFFF);
|
|
54
|
+ randomHexDigit: function() {
|
|
55
|
+ return randomElement(HEX_DIGITS);
|
66
|
56
|
},
|
67
|
57
|
/**
|
68
|
|
- * Generates hex number with length 12
|
|
58
|
+ * Returns a random string of hex digits with length 'len'.
|
|
59
|
+ * @param len the length.
|
69
|
60
|
*/
|
70
|
|
- random12digitsHex: function () {
|
71
|
|
- return rangeRandomHex(0x100000000000, 0xFFFFFFFFFFFF);
|
|
61
|
+ randomHexString: function (len) {
|
|
62
|
+ var ret = '';
|
|
63
|
+ while (len--) {
|
|
64
|
+ ret += this.randomHexDigit();
|
|
65
|
+ }
|
|
66
|
+ return ret;
|
72
|
67
|
}
|
73
|
68
|
};
|
74
|
69
|
|