I want to write a program that creates say 100 squares, with the following rules:
So basically every time I run the App I'll get totally different results. For example, the first time I might get:
The next time I might get:
And so on.
I tried doing this using random numbers but every single time I get the absolute max # from each color category - I basically never had any classes in Probability (or Statistics) so I really have no idea what I'm doing here - thought maybe someone could point me in the right direction or knows how to do Probability in Swift code...?
This function will return an array of 100 randomized UIColors
matching your probability requirement. This code includes a probability of zero for each of the three main colors. If you want to include a probability of at least one of each color then change the range to 1...n
func generateRandomColors() -> [UIColor] {
var randomColors = [UIColor]()
randomColors += Array(repeating: .blue, count: .random(in: 0...10))
randomColors += Array(repeating: .red, count: .random(in: 0...15))
randomColors += Array(repeating: .yellow, count: .random(in: 0...25))
randomColors += Array(repeating: .orange, count: 100 - randomColors.count)
return randomColors.shuffled()
}