问题
Does swift by default has a random number generator that returns the same number only once?
For example it picks a number in range 1,2,3,4,5. Return that random number (lets say 3). In the next loop, it picks only from 1,2,4,5 and so on.
EDIT: This is what I ended up using. It returns an array of integers from 0-5 and was tested in playground. Note that this is meant to be used when you are picking from a large set of Integers and not just 6.
func generateRandomArray() -> [Int]{
var randomImages: [Int] = [Int]()
var newRandomNumber = Int(arc4random_uniform(UInt32(6)))
while (randomImages.count < 6) {
if (randomImages.contains(newRandomNumber) == false){
randomImages.append(newRandomNumber)
}
newRandomNumber = Int(arc4random_uniform(UInt32(6)))
}
return randomImages
}
回答1:
Just store the last random number in a variable and check the next random number against it in a while
loop. While the new random number is the same as the last random number, generate a new random number. Once the new random number generated is not the same as the last random number the while
loop ends and we update the last random number.
var lastRandomNumber = 0
var newRandomNumber = 0
func gernerateRandomNumber() {
newRandomNumber = Int(arc4random_uniform(5)+1)
while newRandomNumber == lastRandomNumber {
newRandomNumber = Int(arc4random_uniform(5)+1)
}
lastRandomNumber = newRandomNumber
}
回答2:
This kind of generator is not called a "random number" generator, but usually a "Shuffle" or "Permute". You have to tell it how many items there are first. Otherwise what you are proposing doesn't make any sense!
see answer here: How do I shuffle an array in Swift?
回答3:
You can do this trick, Add the range of numbers from which you want to get the random result..to an NSMutableArray
or NSMutableSet
(to make sure there is only 1 of it).
Iterate through the array-
for(int i=0;i<myMutableArray .count;i++){
int randomIndex = arc4random_uniform(myMutableArray.count);
int myNumber = [[myMutableArray objectAtIndex:randomIndex]intValue]; // edited
NSLog(@"My random number-%i",myNumber);//this is your generated random number
[myMutableArray removeObjectAtIndex:randomIndex];
}
I guess this would do the tric, But if you do rmember NSMutableArray
cannot take Primitive data type, just Objects like NSNumber
.
EDIT
this line is not written and is justifying that why i am converting the NSNumber back to an interger because while adding the integer in the Array it has to be converted to a NSNumber
|
[ myMutableArray addObject:[NSNumber numberWithInt:2]];
So in the for loop when i am getting the object from myMutableArray
i am getting an NSNumber and to simplify it i casted that Object (NSNumber object) back to an integer.
回答4:
From Apple's documentation:
A set stores distinct values of the same type in a collection with no defined ordering.
This seems to be exactly what you are after, so making an array seems a bit ill-advised...
var values : Set<Int> = []
var value : Int {
return Int(arc4random() % 6 + 1)
}
while values.count < 6 {
values.insert(value)
}
And you probably want to have a look at those values somewhere down the line:
for value in values {
println(value)
}
来源:https://stackoverflow.com/questions/32411482/random-number-generator-function-that-doesnt-repeat-itself