cloudroam
2025-02-14 2fce91b8c0faf1290d8a35ee022dab3cdbc28a54
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package com.example.firstapp.utils
 
import android.graphics.Color
import android.text.TextUtils
import java.util.*
 
/**
 * <pre>
 * desc   : Random Utils
 * author : xuexiang
 * time   : 2018/4/28 上午12:41
</pre> *
 *
 * Shuffling algorithm
 *  * [.shuffle] Shuffling algorithm, Randomly permutes the specified array using a default source of
 * randomness
 *  * [.shuffle] Shuffling algorithm, Randomly permutes the specified array
 *  * [.shuffle] Shuffling algorithm, Randomly permutes the specified int array using a default source of
 * randomness
 *  * [.shuffle] Shuffling algorithm, Randomly permutes the specified int array
 *
 *
 * get random int
 *  * [.getRandom] get random int between 0 and max
 *  * [.getRandom] get random int between min and max
 *
 *
 * get random numbers or letters
 *  * [.getRandomCapitalLetters] get a fixed-length random string, its a mixture of uppercase letters
 *  * [.getRandomLetters] get a fixed-length random string, its a mixture of uppercase and lowercase letters
 *
 *  * [.getRandomLowerCaseLetters] get a fixed-length random string, its a mixture of lowercase letters
 *  * [.getRandomNumbers] get a fixed-length random string, its a mixture of numbers
 *  * [.getRandomNumbersAndLetters] get a fixed-length random string, its a mixture of uppercase, lowercase
 * letters and numbers
 *  * [.getRandom] get a fixed-length random string, its a mixture of chars in source
 *  * [.getRandom] get a fixed-length random string, its a mixture of chars in sourceChar
 *
 *
 */
@Suppress("MemberVisibilityCanBePrivate", "unused")
class RandomUtils private constructor() {
    companion object {
        private const val NUMBERS_AND_LETTERS =
            "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        private const val NUMBERS = "0123456789"
        private const val LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        private const val CAPITAL_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        private const val LOWER_CASE_LETTERS = "abcdefghijklmnopqrstuvwxyz"
 
        /**
         * 在数字和英文字母中获取一个定长的随机字符串
         *
         * @param length 长度
         * @return 随机字符串
         * @see RandomUtils.getRandom
         */
        @JvmStatic
        fun getRandomNumbersAndLetters(length: Int): String? {
            return com.example.firstapp.utils.RandomUtils.Companion.getRandom(
                com.example.firstapp.utils.RandomUtils.Companion.NUMBERS_AND_LETTERS,
                length
            )
        }
 
        /**
         * 在数字中获取一个定长的随机字符串
         *
         * @param length 长度
         * @return 随机数字符串
         * @see RandomUtils.getRandom
         */
        fun getRandomNumbers(length: Int): String? {
            return com.example.firstapp.utils.RandomUtils.Companion.getRandom(
                com.example.firstapp.utils.RandomUtils.Companion.NUMBERS,
                length
            )
        }
 
        /**
         * 在英文字母中获取一个定长的随机字符串
         *
         * @param length 长度
         * @return 随机字母字符串
         * @see RandomUtils.getRandom
         */
        fun getRandomLetters(length: Int): String? {
            return com.example.firstapp.utils.RandomUtils.Companion.getRandom(
                com.example.firstapp.utils.RandomUtils.Companion.LETTERS,
                length
            )
        }
 
        /**
         * 在大写英文字母中获取一个定长的随机字符串
         *
         * @param length 长度
         * @return 随机字符串 只包含大写字母
         * @see RandomUtils.getRandom
         */
        fun getRandomCapitalLetters(length: Int): String? {
            return com.example.firstapp.utils.RandomUtils.Companion.getRandom(
                com.example.firstapp.utils.RandomUtils.Companion.CAPITAL_LETTERS,
                length
            )
        }
 
        /**
         * 在小写英文字母中获取一个定长的随机字符串
         *
         * @param length 长度
         * @return 随机字符串 只包含小写字母
         * @see RandomUtils.getRandom
         */
        fun getRandomLowerCaseLetters(length: Int): String? {
            return com.example.firstapp.utils.RandomUtils.Companion.getRandom(
                com.example.firstapp.utils.RandomUtils.Companion.LOWER_CASE_LETTERS,
                length
            )
        }
 
        /**
         * 在一个字符数组源中获取一个定长的随机字符串
         *
         * @param source 源字符串
         * @param length 长度
         * @return
         *  * if source is null or empty, return null
         *  * else see [RandomUtils.getRandom]
         *
         */
        fun getRandom(source: String, length: Int): String? {
            return if (TextUtils.isEmpty(source)) null else com.example.firstapp.utils.RandomUtils.Companion.getRandom(
                source.toCharArray(),
                length
            )
        }
 
        /**
         * 在一个字符数组源中获取一个定长的随机字符串
         *
         * @param sourceChar 字符数组源
         * @param length     长度
         * @return
         *  * if sourceChar is null or empty, return null
         *  * if length less than 0, return null
         *
         */
        fun getRandom(sourceChar: CharArray?, length: Int): String? {
            if (sourceChar == null || sourceChar.isEmpty() || length < 0) {
                return null
            }
            val str = StringBuilder(length)
            val random = Random()
            for (i in 0 until length) {
                str.append(sourceChar[random.nextInt(sourceChar.size)])
            }
            return str.toString()
        }
 
        /**
         * get random int between 0 and max
         *
         * @param max 最大随机数
         * @return
         *  * if max <= 0, return 0
         *  * else return random int between 0 and max
         *
         */
        fun getRandom(max: Int): Int {
            return com.example.firstapp.utils.RandomUtils.Companion.getRandom(0, max)
        }
 
        /**
         * get random int between min and max
         *
         * @param min 最小随机数
         * @param max 最大随机数
         * @return
         *  * if min > max, return 0
         *  * if min == max, return min
         *  * else return random int between min and max
         *
         */
        fun getRandom(min: Int, max: Int): Int {
            if (min > max) {
                return 0
            }
            return if (min == max) {
                min
            } else min + Random().nextInt(max - min)
        }
 
        /**
         * 获取随机颜色
         *
         * @return
         */
        val randomColor: Int
            get() {
                val random = Random()
                val r = random.nextInt(256)
                val g = random.nextInt(256)
                val b = random.nextInt(256)
                return Color.rgb(r, g, b)
            }
 
        /**
         * 随机打乱数组中的内容
         *
         * @param objArray
         * @return
         */
        fun shuffle(objArray: Array<Any?>?): Boolean {
            return if (objArray == null) {
                false
            } else com.example.firstapp.utils.RandomUtils.Companion.shuffle(
                objArray,
                com.example.firstapp.utils.RandomUtils.Companion.getRandom(objArray.size)
            )
        }
 
        /**
         * 随机打乱数组中的内容
         *
         * @param objArray
         * @param shuffleCount
         * @return
         */
        private fun shuffle(objArray: Array<Any?>?, shuffleCount: Int): Boolean {
            var length = 0
            if (objArray == null || shuffleCount < 0 || objArray.size.also {
                    length = it
                } < shuffleCount) {
                return false
            }
            for (i in 1..shuffleCount) {
                val random = com.example.firstapp.utils.RandomUtils.Companion.getRandom(length - i)
                val temp = objArray[length - i]
                objArray[length - i] = objArray[random]
                objArray[random] = temp
            }
            return true
        }
 
        /**
         * 随机打乱数组中的内容
         *
         * @param intArray
         * @return
         */
        fun shuffle(intArray: IntArray?): IntArray? {
            return if (intArray == null) {
                null
            } else com.example.firstapp.utils.RandomUtils.Companion.shuffle(
                intArray,
                com.example.firstapp.utils.RandomUtils.Companion.getRandom(intArray.size)
            )
        }
 
        /**
         * 随机打乱数组中的内容
         *
         * @param intArray
         * @param shuffleCount
         * @return
         */
        fun shuffle(intArray: IntArray?, shuffleCount: Int): IntArray? {
            var length = 0
            if (intArray == null || shuffleCount < 0 || intArray.size.also {
                    length = it
                } < shuffleCount) {
                return null
            }
            val out = IntArray(shuffleCount)
            for (i in 1..shuffleCount) {
                val random = com.example.firstapp.utils.RandomUtils.Companion.getRandom(length - i)
                out[i - 1] = intArray[random]
                val temp = intArray[length - i]
                intArray[length - i] = intArray[random]
                intArray[random] = temp
            }
            return out
        }
    }
 
    /**
     * Don't let anyone instantiate this class.
     */
    init {
        throw Error("Do not need instantiate!")
    }
}