-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Array+Random.swift
33 lines (29 loc) 路 981 Bytes
/
Array+Random.swift
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
extension FixedWidthInteger {
public static func random() -> Self {
return Self.random(in: .min ... .max)
}
public static func random<T>(using generator: inout T) -> Self
where T : RandomNumberGenerator
{
return Self.random(in: .min ... .max, using: &generator)
}
}
extension Array where Element: FixedWidthInteger {
public static func random(count: Int) -> [Element] {
var array: [Element] = .init(repeating: 0, count: count)
(0..<count).forEach { array[$0] = Element.random() }
return array
}
public static func random<T>(count: Int, using generator: inout T) -> [Element]
where T: RandomNumberGenerator
{
var array: [Element] = .init(repeating: 0, count: count)
(0..<count).forEach { array[$0] = Element.random() }
return array
}
}
extension Array where Element == UInt8 {
public var base64: String {
Data(self).base64EncodedString()
}
}