Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am very new to Swift and trying to learn.

How do I pick 5 random number from a range of 1 to 100 and not repeat a number that was previously picked?

What I have tried:

Don't know where to start. I have been trying to learn using a Udemy course and youtube videos
Posted
Updated 28-Oct-18 11:47am

1 solution

Here's my solution:

import UIKit

var repeatCounter = 1
var pickedNumbers = [Int]()

repeat {
    
    let rangeValue = Int.random(in: 1...100)
    
    if pickedNumbers.contains(rangeValue) {
        
        print("\(rangeValue) already exists in the array")
    
    } else {

        pickedNumbers.append(rangeValue)
        print([pickedNumbers])
        repeatCounter += 1
    }

} while repeatCounter < 6
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900