这可能是愚蠢的问题。我想知道在Swift中使用arc4random_uniform是否可以实现不同的细微差别/随机程度。这是一个示例:
let number = arc4random_uniform(10) + 1
print(number)
在这种情况下,将从1到10随机打印一个数字。但是,有没有办法我可以将随机结果重复2到3次?结果将是这样的:
1, 1, 6, 6, 6, 3, 3, 8, 8, 9, 9, 9 ...
// 1) Randomly selected and 2) repeated 2 to 3 times randomly.
也许我可以一起使用两个arc4random_uniform函数,但是不能正确地表达它们。如果您能给我一些建议,将不胜感激。 <3
答案 0 :(得分:2)
关于随机数生成器的细微差别:请看GKRandomSource。
您在这里所做的并不是真正减少随机性,或修改随机数生成器中的参数。您只是将一个操作(带有一个随机参数)应用于随机整数的集合。
extension Collection {
func duplicateItemsRandomly(range: CountableClosedRange<Int>) -> [Element] {
return self.reduce(into: [Element](), { (acc, element) in
let distance = UInt32(range.upperBound - range.lowerBound + 1)
let count = Int(arc4random_uniform(distance) + UInt32(range.lowerBound))
let result = Array.init(repeating: element, count: count)
acc.append(contentsOf: result)
})
}
}
let sequence = [1, 6, 3, 8, 9]
sequence.duplicateItemsRandomly(range: 2...3)
// [1, 1, 6, 6, 6, 3, 3, 3, 8, 8, 8, 9, 9, 9]
P.S:如果您是在Swift 4.2中编写此代码,请使用Int.random(in:)
。
答案 1 :(得分:2)
为此,您将需要生成两个值:您的随机value
和repeatCount
。另外,您需要记住这两个值,以便您可以重复value
。您可以使用自定义类来做到这一点:
class RandomWithRepeats {
var range: ClosedRange<Int>
var repeatRange: ClosedRange<Int>
var repeatCount = 0
var value = 0
init(range: ClosedRange<Int>, repeatRange: ClosedRange<Int>) {
self.range = range
self.repeatRange = repeatRange
}
// generate a random number in a range
// Just use Int.random(in:) with Swift 4.2 and later
func random(in range: ClosedRange<Int>) -> Int {
return Int(arc4random_uniform(UInt32(range.upperBound - range.lowerBound + 1))) + range.lowerBound
}
func nextValue() -> Int {
// if repeatCount is 0, its time to generate a new value and
// a new repeatCount
if repeatCount == 0 {
// For Swift 4.2, just use Int.random(in:) instead
value = self.random(in: range)
repeatCount = self.random(in: repeatRange)
}
repeatCount -= 1
return value
}
}
示例:
let rand = RandomWithRepeats(range: 1...10, repeatRange: 2...3)
// generate 20 random repeated numbers
for _ in 1...20
{
print(rand.nextValue(), terminator: " ")
}
6 6 6 8 8 8 10 10 10 2 2 9 9 5 5 8 8 8 5 5
答案 2 :(得分:1)
我建议使用自定义Sequence
:
class RepeatingRandomSequence : Sequence {
let rangeLow, rangeSpan : UInt32
let repeatLow, repeatSpan : UInt32
init(range:Range<UInt32>, count:Range<UInt32>) {
rangeLow = range.lowerBound
rangeSpan = range.upperBound - range.lowerBound + 1
repeatLow = count.lowerBound
repeatSpan = count.upperBound - count.lowerBound + 1
}
func makeIterator() -> AnyIterator<UInt32> {
var count : UInt32 = 0
var value : UInt32 = 0
return AnyIterator {
if(count <= 0) {
count = arc4random_uniform(self.repeatSpan) + self.repeatLow
value = arc4random_uniform(self.rangeSpan) + self.rangeLow
}
defer { count = count - 1 }
return value
}
}
}
let sequence = RepeatingRandomSequence(range: 0..<10, count: 2..<3)
let randoms = sequence.makeIterator()
请注意,迭代器randoms
现在使用randoms.next()
生成了一个无限的随机数序列。由于该序列是无限的,所以许多事情并不是特别有用,例如sort
,{{ 1}}等。不过,您可以像这样使用它:
map
或更通常地,例如:
for value in random {
print(value)
if(value == 9) { // or any other termination condition
break
}
}