From ac33f86712122e42e88f981afa5fb9a6823962a0 Mon Sep 17 00:00:00 2001 From: Alex Binary Date: Mon, 6 Jul 2020 20:43:07 +0200 Subject: [PATCH] Add static `.random(in:)` method (#9) Co-authored-by: Sindre Sorhus --- Sources/Percentage/Percentage.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Sources/Percentage/Percentage.swift b/Sources/Percentage/Percentage.swift index dbd50e7..5344962 100644 --- a/Sources/Percentage/Percentage.swift +++ b/Sources/Percentage/Percentage.swift @@ -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.init(fraction: .random(in: range.lowerBound.fraction...range.upperBound.fraction)) + } } extension Percentage: RawRepresentable {