Swift 3 - Make a Random number generate in between 2 integer variables [duplicate] - swift

This question already has answers here:
How does one make random number between range for arc4random_uniform()?
(18 answers)
Closed 5 years ago.
i'm semi new to xcode and swift, and i'm just making some small apps to play around with some stuff. I wanted to try and make a number generator in which you set the minimum number and maximum number and the app will pick a random number in between the two. Would this be possible?
I all that I need help with is making a random number appear in between two variable integers just to clarify.
Thanks

Swift 4.2 Edit:
func randomBetween(min: Int, max: Int) -> Int {
return Int.random(in: min ..< max)
}
import GameKit
func randomBetween(min: Int, max: Int) -> Int {
return GKRandomSource.sharedRandom().nextInt(upperBound: max - min) + min
}

Related

how to convert hour to seconds in swift? [duplicate]

This question already has answers here:
Swift - Integer conversion to Hours/Minutes/Seconds
(25 answers)
Closed 3 months ago.
I need to convert this code to an hours
func convert_to_seconds(hours: Int) -> Int {
// write your code here
}
the input is:
hours = 6
the output is:
21600
I tried several codes but they didn't work for me
func convert_to_seconds(hours: Int) -> Int {
hours*3600
}

How to stop rounding in NSDecimalNumber? [duplicate]

This question already has answers here:
In Swift 3, how to calculate the factorial when the result becomes too high?
(2 answers)
BigInteger equivalent in Swift?
(6 answers)
Closed 3 years ago.
I am solving a question from HackerRank which asks to print the value of extra-long factorials that can't be stored even in a 64-bit long variable.
I am using NSDecimalNumber to store the value. However, even in this case, the final result is rounded off.
func extraLongFactorials(n: Int) -> Void
{
var factorial: NSDecimalNumber = 1
for index in 1...n
{
let indexInNSDecimal = NSDecimalNumber(value: index)
factorial = factorial.multiplying(by: indexInNSDecimal)
}
let factorialWithoutRounding = factorial.description(withLocale: nil)
print(factorialWithoutRounding)
}
print(extraLongFactorials(n: 45)) // 119622220865480194561963161495657715064000000000000000000
However, the result should be 119622220865480194561963161495657715064383733760000000000.
This link talks about using description(withLocale:).
NSDecimalNumber round long numbers
However, it does not clearly explain how to use the description(withLocale:) method.
I also went through the apple doc https://developer.apple.com/documentation/foundation/nsdecimalnumber/1412789-description. But it also does not explain clearly how to use it.
Can someone please discuss this method in detail.

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 to generate random numbers without repetition in Swift? [duplicate]

This question already has answers here:
Unique (non-repeating) random numbers in O(1)?
(22 answers)
How do I shuffle an array in Swift?
(25 answers)
Closed 8 years ago.
How can I generate random numbers between 0 and 31 without call the number twice in Swift Language ?
You can create a function that returns a random-number generating closure, like this:
func randomSequenceGenerator(min: Int, max: Int) -> () -> Int {
var numbers: [Int] = []
return {
if numbers.isEmpty {
numbers = Array(min ... max)
}
let index = Int(arc4random_uniform(UInt32(numbers.count)))
return numbers.remove(at: index)
}
}
To use it, first call the generator once to create the random sequence generator, then call the return value as many times as you like. This will print out random values between one and six, cycling through all of them before starting over:
let getRandom = randomSequenceGenerator(min: 1, max: 6)
for _ in 1...20 {
print(getRandom())
}

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.