Swift semi random selection [duplicate] - swift

This question already has answers here:
Generate random numbers with a given distribution
(4 answers)
Closed 7 years ago.
I want to "randomly" select from a collection in Swift but have certain instances be selected at different frequencies. Currently I am using.
let collection: [Int] = [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]
let random: Int = Int(arc4random_uniform(UInt32(collection.count)))
let selection: Int = collection[random]
I need the code to be compliant with Swift 2.0+.

If you're able to target iOS 9.0 or later, GameplayKit has a great solution called GKGaussianDistribution, which lets you generate random numbers in a range where the frequency of each number matches a bell curve like this:
To use this, just import GameplayKit into your project, then use this code:
let distribution = GKGaussianDistribution.d6()
print(distribution.nextInt())
That uses a six-sided die, but you can use any range you want. You might find my tutorial on GameplayKit random number generation useful.

Related

Swift random element with probability [duplicate]

This question already has answers here:
Generate random numbers with a given distribution
(4 answers)
how to generate random numbers from 1 to 4 and 8 in swift
(2 answers)
Closed 4 months ago.
It seems like randomElement() in Swift only allows for an equally weighted selection. What would be the best (and most efficient way, as I need to repeat this very often) to select from a collection with pre-specified probabilities/weights?

Seeing rounding errors (e.g. 0.22999999 instead of 0.23) in Swift 2.2 [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I'm new to swift (using swift 2.2) and I'm seeing an issue where 200.23 - 200 is shown to be equal to 0.2299999999999898. I know that computers can't represent irrational numbers accurately, but I don't understand why a rational number is also not being represented correctly. This is affecting the output numbers generated by my program, leading to issues.
Here's a sample playground code (a very simple version of part of my actual code) showing this behavior:
var number:Double = 200.23 //Number I'm testing
var left: Double = floor(number) // Extracting integer part
var right: Double = number-left // Extracting fraction part
In the above code I'm defining a number and then extracting its whole and fractional parts. The fractional part should be 0.23, however it's shown as 0.2299999999999898 in swift's playground.
Similarly, if I change the original number from 200.23 to 100.23, then the fractional part is shown to be 0.230000000000004
How can I solve this issue ?
EDIT:
I've been asked about the context of this problem. I'm designing a statistics app that uses numbers a lot and actually has a small builtin calculator too. So if I user tries to calculate 200.23 - 0.23 and sees anything other than 0.23, he's gonna be surprised!
To round the values stored in the variable right just use the round() function. To specify the precision just do this
var number:Double = 200.23
var left: Double = floor(number)
var right: Double = number-left
let roundedValue = round(right*100)/100 // Answer will be 0.23

Swift rand is not generating random numbers [duplicate]

This question already has answers here:
Swift rand() not being random
(3 answers)
Closed 6 years ago.
I put rand() in Xcode Playground and it prints the same number 16807 even I run many times and value did not change -> "16807"!
is this a bug?
print(rand())
Mohamed Diaa,
You can make use of library functions like arc4random() or arc4random_uniform() for generating random numbers.
let random = Int(arc4random_uniform(3))
To get random value from 0 to 3
you might have to import Darwin to use it.

How to generate random numbers in Swift 2 Xcode 7? [duplicate]

This question already has an answer here:
arc4random_uniform not available in Xcode 7.0 beta (7a176x) on OSX 10.10.4
(1 answer)
Closed 7 years ago.
Have updated to Xcode 7, which has Swift 2. I am creating an app. I need to generate random numbers. In Swift, there used to be arc4random(). But in Swift 2, arc4random() is not there. Instead, I am getting:
arc4random_addrandom(<#T##UnsafeMutablePointer<UInt8>#>, <#T##Int32#>)
arc4random_stir()
arc4random_buf(<#T##UnsafeMutablePointer<Void>#>, <#T##Int#>)
How do I now generate random numbers? Any help would be appreciated a lot.
arc4random() and arc4random_uniform(_:) are actually there, they're just not autocompleting for some reason. You can still use them!

How to set a number in a couple of cells within a cell array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Assign a value to multiple cells in matlab
I am trying to enter a number, lets say 3, into all the cells in column 2 that are empty.
Like this:
emptyList = cellfun(#isempty,anscell)
anscell{emptyList(:,2),2}=3
but I get this message that
The right hand side of this assignment has too few values to satisfy the left hand side.
Can I overcome it without loops and creating sum and ones functions?
Is this what you need?
anscell = cell(3,2)
emptyList = cellfun(#isempty,anscell)
anscell(emptyList(:,2),2)={3}
Does this do what you want to do?
[anscell{emptyList(:,2),2}] = deal(3)