Skip to content

Commit

Permalink
Add static .random(in:) method (#9)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
alexbinary and sindresorhus committed Jul 6, 2020
1 parent 02d818c commit ac33f86
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/Percentage/Percentage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ public struct Percentage: Hashable, Codable {
```
*/
public func of(_ value: Double) -> Double { value * rawValue / 100 }

/**
Returns a random value within the given range.
```
Percent.random(in: 10%...20%)
//=> Can be 10%, 11%, 12%, 19.98%, etc.
```
*/
public static func random(in range: ClosedRange<Self>) -> Self {
self.init(fraction: .random(in: range.lowerBound.fraction...range.upperBound.fraction))
}
}

extension Percentage: RawRepresentable {
Expand Down

0 comments on commit ac33f86

Please sign in to comment.