Two different random generated numbers in Swift [duplicate] - swift

This question already has answers here:
How to generate a random number in Swift without repeating the previous random number?
(5 answers)
Closed 5 years ago.
I want that my two random generated labels does not generate the same number like 5 and 5
I have done everything else but this
else if rightScoreLabel == leftScoreLabel {
// what goes here?
{
sorry i’m starter

Try approaching this another way: keep generating new numbers until you have two different values.
var a = 0
var b = 0
while a == b {
a = Int(arc4random_uniform(10))
b = Int(arc4random_uniform(10))
}
Alternatively, you can regenerate just one of the numbers:
var a = Int(arc4random_uniform(10))
var b = 0
repeat {
b = Int(arc4random_uniform(10))
} while a == b

Related

Swift Higher order functions [duplicate]

This question already has answers here:
Beginner Swift 3: How to find pairs in array that add up to given number
(4 answers)
Closed 4 months ago.
I have the following problem,
I want to get the pair of given sum by using higher order functions. I am able to do this using iterative approach. Can someone help me to solve the problem using Swift higher order functions like map, filter etc.
let array = [1,2,3,4,5]
let givenSum = 9
for i in 0..<array.count {
let j = i + 1
for j in j..<array.count {
if array[i] + array[j] == givenSum {
print("Pair : \(array[i]),\(array[j])")
}
}
}
The output is [4,5]
Any help is appreciated. Thank you
let array = [1,5,2,3,4]
let givenSum = 9
let resultArray = array.sorted().filter{ array.firstIndex(of: givenSum-$0) ?? -1 > array.firstIndex(of: $0)!}.map{ ($0 , givenSum - $0)}
print((resultArray))
The above code gives you the expected result with higher order functions (I did not check it for lots of cases).
#Ajay K I think we need to use for loop in any way to implement this solution
Still, you can use the following improved solution
var map = [Int: Int]()
for (i, n) in array.enumerated() {
let diff = givenSum - n
if let j = map[diff] {
print("Pair : \(array[i]),\(array[j])")
}
map[n] = i
}
Happy Coding. Open for thoughts!

Return to beginning of a function in Swift [duplicate]

This question already has answers here:
Generate a Swift array of nonrepeating random numbers
(3 answers)
Closed 1 year ago.
So I've following code running in Xcode 12 with SwiftUI:
public func giveCard() -> String {
var randomNumber = Int.random(in: 0..<maxCards-1)
if saveRandoms.contains(randomNumber) {
print("Already exist - generate new number")
} else {
let actualCard = questionCard[randomNumber]
saveRandoms.add(randomNumber)
return actualCard
print("Schicke frage")
}
return "No question-card left"
}
The code creates random numbers between 0 and the variable maxCards.
When a questionCard fitting to the random number which was created was used, the random number is stored in the array saveRandoms.
To avoid showing the same question the if-statement checks if the new created random number is already in the array saveRandoms. If not, the fitting question will be shown.
If the number was already used it should generate a new number until finding a unused one.
And that's my problem. How can I return to the beginning of my function to check again and again if the random number was already used?
I think the best solution is to put the logic in a while loop and finish executing when a new number is found.

Swift 4: modifying numbers in String [duplicate]

This question already has answers here:
Leading zeros for Int in Swift
(12 answers)
Closed 5 years ago.
In my application i create multiple files and i would like them to have consecutive numbering as part of the name. For example log_0001, log_0002
I use string interpolation, so in code it looks like
fileName = "log_\(number)"
number = number + 1
However, if i keep number as just Int, i will have log_1 instead of log_0001
I've only figured that i could check if number has 1/2/3/4 digits and add '000/00/0' manually.
Are there any String modifications allowing to put the required number of '0' to make it 4 symbols?
"I've only figured that i could check if number has 1/2/3/4 digits and add '000/00/0' manually."
In this case try the following code: Save the digit length in a variable, for example digitLength.
if (digitLength == 1) { print("log_000")
} else if (digitLength == 2) { print("log_00")
} else if (digitLength == 3) { print("log_0")
}
else { print("log_") }
print(number)
and then the variable.

Generate a random number in swift and then add another number to it? [duplicate]

This question already has answers here:
How to generate a random number in Swift?
(26 answers)
Closed 7 years ago.
How do I generate a random number in Swift language that can be used in math, such as addition. Basically I just want to be able to generate a random number and then add it to a count that I have.
For example, how would I generate a random number that I can add 1 to?
Use arc4random_uniform() function because it generates a uniform distribution.
The following line generates a number from 0-9.
var x = Int(arc4random_uniform(10)) // Cast it to Int because function returns UInt32
println(x)
var sum = 10 + x
println(sum)
Try to use this one & the arc4random function will generate value between 1-9 & it returns UInt32 type value so modify it what you wanna.
var count : UInt32 = 10
var value : UInt32 = arc4random()%10
count += value
print(count)

How do you generate a random number in swift? [duplicate]

This question already has answers here:
How to generate a random number in Swift?
(26 answers)
Closed 8 years ago.
tl:dr; How do I generate a random number, because the method in the book picks the same numbers every time.
This seems to be the way in Swift to generate a random number, based on the book released from Apple.
protocol RandomNumberGenerator {
func random() -> Double
}
class LinearCongruentialGenerator: RandomNumberGenerator {
var lastRandom = 42.0
let m = 139968.0
let a = 3877.0
let c = 29573.0
func random() -> Double {
lastRandom = ((lastRandom * a + c) % m)
return lastRandom / m
}
}
let generator = LinearCongruentialGenerator()
for _ in 1..10 {
// Generate "random" number from 1-10
println(Int(generator.random() * 10)+1)
}
The problem is that in that for loop I put at the bottom, the output looks like this:
4
8
7
8
6
2
6
4
1
The output is the same every time, no matter how many times I run it.
The random number generator you created is not truly random, it's psueodorandom.
With a psuedorandom random number generator, the sequence depends on the seed. Change the seed, you change the sequence.
One common usage is to set the seed as the current time, which usually makes it random enough.
You can also use the standard libraries: arc4random(). Don't forget to import Foundation.
Pseudorandom number generators need a "seed" value. In your case, if you change lastRandom with any number, you'll get a different sequence.