-2

i want to use arc4random but produced Numbers are must different each other.

How do i do this example?

  • 2
    Put your numbers into an array. Shuffle it. Remove the numbers in order. See: [How to shuffle an array in Swift](https://stackoverflow.com/a/24029847/1630618) – vacawama Aug 26 '18 at 21:25
  • 1
    Can you show us the code that you tried and explain why it is not giving you what you wanted? – Steve Robertson Aug 26 '18 at 22:04
  • i don't want to shuffle numbers in array. i want to different produced numbers in array. – B.Tastekin Aug 26 '18 at 22:06
  • Do you want random numbers in the full range of `arc4random()` (from 0 to 2³²). If not, you should use `arc4random_uniform(_:)` to limit the range of numbers. – Duncan C Aug 26 '18 at 23:23

3 Answers3

0

You can use Set to easily achieve this:

var randomSet: Set<Int> = [] //Sets guarantee uniqueness
var array: [Int] = [] //Use arrays to guarantee the order
while randomSet.count < 2 {
    randomSet.insert(Int(arc4random())) //Whatever random number generator you use
}
for number in randomSet {
    array.append(number)
}
//Now you can use the array for your operations
Xcoder
  • 1,433
  • 3
  • 17
  • 37
  • This is perfect. Thank you – B.Tastekin Aug 26 '18 at 22:49
  • Note that using a set you will lose the original order – Leo Dabus Aug 26 '18 at 23:16
  • After creating the set of numbers, why not use `var array = Array(randomSet)`? (The `Array` type has an initializer that takes a `Set` as it's input.) – Duncan C Aug 26 '18 at 23:21
  • @DuncanC this will discard the original order which the elements where inserted – Leo Dabus Aug 26 '18 at 23:23
  • @LeoDabus true, but does the original order matter with *random* numbers? If you want to preserve the original order you could use a set to validate the uniqueness and add one number at a time to your array. – Duncan C Aug 26 '18 at 23:35
  • What is memberAfterInsert? Where is it defined? I can't find it. – Duncan C Aug 26 '18 at 23:39
  • It is one of the values returned by the set insert method. `mutating func insert(_ newMember: Int) -> (inserted: Bool, memberAfterInsert: Int)` – Leo Dabus Aug 26 '18 at 23:41
0

How about this:

let desired = 20
var randomSet: Set<Int> = [] //Sets guarantee uniqueness
var array: [Int] = [] //Use arrays to guarantee the order

while array.count < desired {
    let random = Int(arc4random())
    if !randomSet.contains(random) {
        array.append(random)
        randomSet.insert(random)
    }
}

Or, using LeoDaubus' suggestion:

while array.count < desired {
    let random = Int(arc4random())
    if randomSet.insert(random).inserted {
        array.append(random)
    }
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

You can use a set to check if the random number was inserted appending the member after checking if it was inserted:

var set: Set<Int> = []
var randomElements: [Int] = []
let numberOfElements = 10
while set.count < numberOfElements {
    let random = Int(arc4random_uniform(10))  //  0...9
    set.insert(random).inserted ? randomElements.append(random) : ()
}
print(randomElements) // "[5, 2, 8, 0, 7, 1, 4, 3, 6, 9]\n"
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571