Skip to content

Commit fd194ee

Browse files
committed
Add method to generate collections
1 parent a6b9ced commit fd194ee

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Sources/AtRandom/AtRandom.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,22 @@ public extension Random where Value: Comparable & Strideable, Value.Stride: Sign
164164
}
165165
}
166166

167+
public extension Random where Value: RangeReplaceableCollection, Value.Element: Comparable & Strideable, Value.Element.Stride: SignedInteger {
168+
/// A property that generates a collection of random values by choosing
169+
/// randomly from a range.
170+
///
171+
/// - Parameters:
172+
/// - range: The range from which the values are selected.
173+
/// - count: The number of values to generate.
174+
public init(in range: ClosedRange<Value.Element>, count: Int) {
175+
self.init { rng in
176+
Value((0 ..< count).map { _ in
177+
range.randomElement(using: &rng) ?? range.lowerBound
178+
})
179+
}
180+
}
181+
}
182+
167183
public extension Random {
168184
/// A property that generates a random value by choosing randomly from a
169185
/// choice of options..

0 commit comments

Comments
 (0)