How to randomize image pattern in Swift? Keep pattern + increase - ios

New to programming and trying to make my first game. It's 4 UIImages that I want to "turn on" in a pattern as the user copies the pattern. From there is follows that same pattern and increases the random pattern. Is arc4random.uniform() what I am looking for here? I know that generates a random number but I'm stuck on how to get the random image to repeat and add +1 random to it.

So you have 4 images, I'm guessing you want a random number between 1 and 4 to represent the images you want to "turn on". The following can generate a random sequence of images for you (you can test this out in a Swift Playground):
import Foundation
let numberOfImages = 4
func addRandom(array: [Int]) -> [Int] {
var returnArray = array
returnArray.append(Int(arc4random()) % numberOfImages)
return returnArray
}
func makePattern(length: Int) -> [Int] {
var pattern = [Int]()
for _ in 0...(length - 1) {
pattern = addRandom(pattern)
}
return pattern
}
// Array of 3 random digits
var firstPattern = makePattern(3)
// Same array as before, with one more random digit at the end
var nextPattern = addRandom(firstPattern)
// Same array as before, with yet another random digit at the end
var lastPattern = addRandom(nextPattern)
Just for fun, you can do this even more cleanly with a Pattern class, like so:
import Foundation
class Pattern {
private(set) var maxRandomNumber: Int!
private(set) var pattern: [Int] = [Int]()
init(maxRandomNumber: Int) {
self.maxRandomNumber = maxRandomNumber
}
init(length: Int, maxRandomNumber: Int) {
self.maxRandomNumber = maxRandomNumber
for _ in 0...(length - 1) {
self.appendRandom()
}
}
func appendRandom() {
self.pattern.append(Int(arc4random()) % self.maxRandomNumber)
}
}
// Initialize a new pattern with 3 iterations and a max random number of 4
var pattern = Pattern(length: 3, maxRandomNumber: 4)
// See the pattern
print(pattern.pattern)
// Add another random number on to the end and check
pattern.appendRandom()
print(pattern.pattern)

Related

Randomly choosing an item from a Swift array without repeating

This code picks a random color from a array of pre-set colors. How do I make it so the same color doesn't get picked more than once?
var colorArray = [(UIColor.redColor(), "red"), (UIColor.greenColor(), "green"), (UIColor.blueColor(), "blue"), (UIColor.yellowColor(), "yellow"), (UIColor.orangeColor(), "orange"), (UIColor.lightGrayColor(), "grey")]
var random = { () -> Int in
return Int(arc4random_uniform(UInt32(colorArray.count)))
} // makes random number, you can make it more reusable
var (sourceColor, sourceName) = (colorArray[random()])
Create an array of indexes. Remove one of the indexes from the array and then use that to fetch a color.
Something like this:
var colorArray = [
(UIColor.redColor(), "red"),
(UIColor.greenColor(), "green"),
(UIColor.blueColor(), "blue"),
(UIColor.yellowColor(), "yellow"),
(UIColor.orangeColor(), "orange"),
(UIColor.lightGrayColor(), "grey")]
var indexes = [Int]();
func randomItem() -> UIColor
{
if indexes.count == 0
{
print("Filling indexes array")
indexes = Array(0..< colorArray.count)
}
let randomIndex = Int(arc4random_uniform(UInt32(indexes.count)))
let anIndex = indexes.removeAtIndex(randomIndex)
return colorArray[anIndex].0;
}
The code above creates an array indexes. The function randomItem looks to see if indexes is empty. if it is, it populates it with index values ranging from 0 to colorArray.count - 1.
It then picks a random index in the indexes array, removes the value at that index in the indexes array, and uses it to fetch and return an object from your colorArray. (It doesn't remove objects from the colorArray. It uses indirection, and removes objects from the indexesArray, which initially contains an index value for each entry in your colorArray.
The one flaw in the above is that after you fetch the last item from indexArray, you populate it with a full set of indexes, and it's possible that the next color you get from the newly repopulated array will be the same as the last one you got.
It's possible to add extra logic to prevent this.
based on the fact, that arc4random_uniform generate not only random, but also uniformly distributed numbers
import Foundation // arc4random_uniform
class Random {
var r:UInt32
let max: UInt32
init(max: UInt32) {
self.max = max
r = arc4random_uniform(max)
}
var next: UInt32 {
var ret: UInt32
repeat {
ret = arc4random_uniform(max)
} while r == ret
r = ret
return r
}
}
// usage example
let r = Random(max: 5)
for i in 0..<10 {
print(r.r, r.next) // there will never be a pair of the same numbers in the
// generated stream
}
/*
2 4
4 0
0 3
3 0
0 3
3 4
4 1
1 3
3 4
4 3
*/
simple test for different k and stream length of one milion
class Random {
var r:UInt32
let max: UInt32
init(max: UInt32) {
self.max = max
r = arc4random_uniform(max)
}
var next: (UInt32, Int) {
var i = 0
var ret: UInt32
repeat {
ret = arc4random_uniform(max)
i += 1
} while r == ret
r = ret
return (r,i)
}
}
for k in 3..<16 {
let r = Random(max: UInt32(k))
var repetition = 0
var sum = 0
for i in 0..<1000000 {
let j = r.next
repetition = max(repetition, j.1)
sum += j.1
}
print("maximum of while repetition for k:", k, "is", repetition, "with average of", Double(sum) / Double(1000000) )
}
prints
maximum of while repetition for k: 3 is 15 with average of 1.499832
maximum of while repetition for k: 4 is 12 with average of 1.334008
maximum of while repetition for k: 5 is 9 with average of 1.250487
maximum of while repetition for k: 6 is 8 with average of 1.199631
maximum of while repetition for k: 7 is 8 with average of 1.167501
maximum of while repetition for k: 8 is 7 with average of 1.142799
maximum of while repetition for k: 9 is 8 with average of 1.124096
maximum of while repetition for k: 10 is 6 with average of 1.111178
maximum of while repetition for k: 11 is 7 with average of 1.099815
maximum of while repetition for k: 12 is 7 with average of 1.091041
maximum of while repetition for k: 13 is 6 with average of 1.083582
maximum of while repetition for k: 14 is 6 with average of 1.076595
maximum of while repetition for k: 15 is 6 with average of 1.071965
finaly, here is more Swifty and functional approach based on the same idea
import Foundation
func random(max: Int)->()->Int {
let max = UInt32(max)
var last = arc4random_uniform(max)
return {
var r = arc4random_uniform(max)
while r == last {
r = arc4random_uniform(max)
}
last = r
return Int(last)
}
}
let r0 = random(8)
let r1 = random(4)
for i in 0..<20 {
print(r0(), terminator: " ")
}
print("")
for i in 0..<20 {
print(r1(), terminator: " ")
}
/*
4 5 4 3 4 0 5 6 7 3 6 7 5 4 7 4 7 2 1 6
0 3 0 1 0 2 3 1 2 0 1 0 1 0 1 3 0 3 0 2
*/
Fill an array with the colors and shuffle it with a Fisher-Yates shuffle. Then use the element at an end, remove it, and insert it at a random position at least n positions from the end.
For example, say my array has 10 elements. I shuffle it and take the last. I want at least 2 values to be chosen before I see it again so I generate a random position in the range 0...8 and insert it there.
var colorArray = [
(UIColor.redColor() , "red" ),
(UIColor.greenColor() , "green" ),
(UIColor.blueColor() , "blue" ),
(UIColor.yellowColor() , "yellow"),
(UIColor.orangeColor() , "orange"),
(UIColor.lightGrayColor(), "grey" )].shuffle() // shuffle() is from my link above
let spacing = 2 // Pick at least 2 colors before we see it again
if let randomColor = colorArray.popLast() {
colorArray.insert(randomColor,
atIndex: Int(arc4random_uniform(UInt32(colorArray.count - spacing))))
}
One case, described here: https://github.com/dimpiax/GenericSequenceType
Another is functional:
func getRandomItem<T>(arr: [T]) -> (unique: Bool) -> T {
var indexes: [Int]!
return { value in
let uniqIndex: Int
if value {
if indexes?.isEmpty != false {
indexes = [Int](0.stride(to: arr.count, by: 1))
}
uniqIndex = indexes.removeAtIndex(Int(arc4random_uniform(UInt32(indexes.count))))
}
else {
uniqIndex = Int(arc4random_uniform(UInt32(arr.count)))
}
return arr[uniqIndex]
}
}
let generate = getRandomItem(colorArray)
generate(unique: true).0 // greenColor
generate(unique: true).0 // redColor
generate(unique: true).0 // lightGrayColor
Try it it's work for me and 100% tested
let arrString = ["1","2","3","4","5","6"]
var selectedIndix = -1
#IBAction func btnClick(_ sender: Any) {
let randomElementIndex = randomElementString()
}
Call this function when button clicked
func randomElementString() -> Int{
let randomm = Int(arc4random_uniform(UInt32(arrString.count)))
if selectedIndix == randomm{
return randomElementString()
}else{
selectedIndix = randomm
return randomm
}
}
OUTPUT:-
5121242316513126
How about running a while loop with the condition:
while(self.source.backgroundColor == sourceColor) {
// get a new random sourceColor
}
This will keep looping until a new random color has been selected.
edit
Additional Note: The point was the while loop. There are ways to safeguard from an infinite loop, it's up to the coder to find the right solution. I don't think SO is a place to write other's code but instead to offer suggestions .. mine is a start.
But since my answer was given such a negative rating, i'll push instead of nudge in the right direction.
The other answers are unnecessarily bloated. And? The one I offered above offers a less than desirable time complexity. So, here's my new answer (in meta code):
// array of all background colors
var arrayOfColors = [..]
// get a random index
var randomIndex = arc4random(size of arrayOfColors)
// select new background color
var newBGColor = arrayOfColors[randomIndex]
// old background color
var oldBGColor = self.source.backgroundColor
// remove new color from array (so that it's excluded from choices next time)
arrayOfColors.removeAtIndex(randomIndex)
// set the new color to the background
self.source.backgroundColor = newBGColor
// add current color back into the pool of potential colors
arrayOfColors.addObject(oldBGColor)

Using arc4random to generate ten random numbers

I am using arc4random to generate 10 random numbers so I can then query firebase to get the questions that contain the randomly generated numbers. The problem is that I don't want any number to appear more than once so then there are not duplicate questions. Current code below...
import UIKit
import Firebase
class QuestionViewController: UIViewController {
var amountOfQuestions: UInt32 = 40
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Use a for loop to get 10 questions
for _ in 1...10{
// generate a random number between 1 and the amount of questions you have
let randomNumber = Int(arc4random_uniform(amountOfQuestions - 1)) + 1
print(randomNumber)
// The reference to your questions in firebase (this is an example from firebase itself)
let ref = Firebase(url: "https://test.firebaseio.com/questions")
// Order the questions on their value and get the one that has the random value
ref.queryOrderedByChild("value").queryEqualToValue(randomNumber)
.observeEventType(.ChildAdded, withBlock: {
snapshot in
// Do something with the question
print(snapshot.key)
})
}
}
#IBAction func truepressed(sender: AnyObject) {
}
#IBAction func falsePressed(sender: AnyObject) {
}
}
You can have an array to store the value you want to random with, in your case, [1,2,3....10], and then use arc4random to get the random index of any value inside (0..9), get the value and remove it from array. Then you will never get the same number from the array.
Given the total number of questions
let questionsCount = 100
you can generate a sequence of integers
var naturals = [Int](0..<questionsCount)
Now given the quantity of unique random numbers you need
let randomsCount = 10
that of course should not exceed the total number of questions
assert(randomsCount <= questionsCount)
you can build your list of unique integers
let uniqueRandoms = (1..<randomsCount).map { _ -> Int in
let index = Int(arc4random_uniform(UInt32(naturals.count)))
return naturals.removeAtIndex(index)
}
As an alternative to generating the random number on the client and requesting a question at that specified number, you could download the entire array of questions and shuffle the array. GameKit provides a built-in method to shuffle the array.
import GameKit
// ...
let ref = Firebase(url: "https://test.firebaseio.com/questions")
// Order the questions on their value and get the one that has the random value
ref.queryOrderedByChild("value")
.observeEventType(.ChildAdded, withBlock: {
snapshot in
// Shuffle your array
let shuffledQuestions = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(snapshot)
// Store your array somewhere and iterate through it for the duration of your game
})
You could generate random numbers and store each number in an NSArray. However, when you append it to the array you can check if the array already contains that number.
For example:
for _ in 1...10 {
let amountOfQuestions: UInt32 = 40
var intArray: [Int] = []
let randomNumber = Int(arc4random_uniform(amountOfQuestions - 1)) + 1
if intArray.contains(randomNumber) {
repeat {
randomNumber = Int(arc4random_uniform(amountOfQuestions - 1)) + 1
} while intArray.contains(randomNumber)
if !intArray.contains(randomNumber) {
intArray.append(randomNumber)
}
} else {
intArray.append(randomNumber)
}
print(intArray)
}
After that you can make a FireBase request with your uniquely generated integers. I hope I was able to help :).
Create unsorted Array of ten random Int 0..99
var set = Set<Int>()
while set.count < 10 {
let num = Int(arc4random_uniform(100))
set.insert(num)
}
let arr = Array(set)

This algorithm takes more time than expected

I have this code, where i would call this "checkingfunction" function. I am not using any threading in my app, I would love to use if it benefits the performance of my app.
The "checkingfunction", takes more time than i expected. It takes more than 30 seconds to complete the execution. I cant wait that long in my app. That is not good, in middle of the game.
Somebody help me out here to rewrite the function, so that i can execute it in a faster way. Some functional programming way, if possible.
func returnCharactersFromAFourLetterString(inputString : String) -> (First : Character,Second : Character, Third : Character, Fourth : Character)
{
return (inputString[advance(inputString.startIndex, 0)],inputString[advance(inputString.startIndex, 1)],inputString[advance(inputString.startIndex, 2)],inputString[advance(inputString.startIndex, 3)])
}
func checkingWords(userEnteredWord : String)
{
var tupleFourLetters = self.returnCharactersFromAFourLetterString(userEnteredWord)
var firstLetter = String(tupleFourLetters.First)
var secondLetter = String(tupleFourLetters.Second)
var thirdLetter = String(tupleFourLetters.Third)
var fourthLetter = String(tupleFourLetters.Fourth)
var mainArrayOfWords : [String] = [] // This array contains around 0.2 million words
var userEnteredTheseWords : [String] = [] // This array contains less than 10 elements
// Check for FirstLetter
for index in 0..<array.count // Array of Letters as Strings , count = 200
{
var input = array[index]
var firstWord = "\(input)\(secondLetter)\(thirdLetter)\(fourthLetter)"
var secondWord = "\(firstLetter)\(input)\(thirdLetter)\(fourthLetter)"
var thirdWord = "\(firstLetter)\(secondLetter)\(input)\(fourthLetter)"
var fourthWord = "\(firstLetter)\(secondLetter)\(thirdLetter)\(input)"
if !contains(userEnteredTheseWords, firstWord) && !contains(userEnteredTheseWords, secondWord) && !contains(userEnteredTheseWords, thirdWord) && !contains(userEnteredTheseWords, fourthWord)
{
if contains(mainArrayOfWords, firstWord )
{
self.delegate?.wordMatchedFromDictionary(firstWord)
return
}
else if contains(mainArrayOfWords, secondWord)
{
self.delegate?.wordMatchedFromDictionary(secondWord)
return
}
else if contains(mainArrayOfWords, thirdWord)
{
self.delegate?.wordMatchedFromDictionary(thirdWord)
return
}
else if contains(mainArrayOfWords, fourthWord)
{
self.delegate?.wordMatchedFromDictionary(fourthWord)
return
}
}
if index == array.count - 1
{
self.delegate?.wordMatchedFromDictionary("NoWord")
}
}
}
Input of this function is a four letter word, Inside this function i am changing each letter by looping through that 200 letters, and checking in the mainArray that, whether any of these changed words exists in mainArray. If exists, then return me that word, otherwise just return NoWord. So totally, we can see that we are checking that contains(mainArray, word) thing around 800 times, i think this is the line which consumes more time, cause mainArray contains 0.2 million words.
Use dictionaries to look up things.
When you measure times, especially with Swift code, measure a release build, not a debug build. On the other hand, measure on the slowest device capable of running your code.

Convert Character to Int in Swift

I'm working on a project which includes verifying the checksum of an Int input with the Damm Algorithm. I've managed to create a the operational table and my method for accessing the value in the table involves passing an interim value and a digit to pass in as the column value.
ex.
self.tableToUse[interim,checkSumArray[i]]
Unfortunately, I've run into a snag when I'm trying to pass the digits from my input into the the get/set method where I cannot find a way to convert the Characters into Ints.
func encode(number: Int) -> Int{
var checkSumArray = [Int]()
if number > 99999999 {
println("number is too large")
return 0
}
else if number < 0 {
println("invalid input")
return 0
}
else {
checkSumArray.append(number%(10))
checkSumArray.append((number%(100)-checkSumArray[0])/10)
checkSumArray.append((number%(1000)-checkSumArray[1])/100)
checkSumArray.append((number%(10000)-checkSumArray[2])/1000)
checkSumArray.append((number%(100000)-checkSumArray[3])/10000)
checkSumArray.append((number%(1000000)-checkSumArray[4])/100000)
checkSumArray.append((number%(10000000)-checkSumArray[5])/1000000)
checkSumArray.append((number%(100000000)-checkSumArray[6])/10000000)
checkSumArray = checkSumArray.reverse()
var interim: Int = 0
for i in 0..<checkSumArray.count{
interim = self.tableToUse[interim,checkSumArray[i]]
}
return interim
}
}
As you can see, I've had to resort to a really nasty way of dealing with this. It works, but it's very limited, inefficient, and just ugly to look at or maintain. I've looked at the option of using Characters instead of Ints in the Damm Table I've constructed and altering the get/set method to deal with those instead, but that's a lot of extra work and could introduce other issues. Any suggestions of alternative ways to handle this, or a way to convert Characters to Ints would be appreciated.
Thanks!
Based on How convert a *positive* number into an array of digits in Swift, you could do (the number has to be positive):
let checkSumArray = map(number.description) { String($0).toInt()! }
if let int = Int(String(Character("1"))) {
print(int)
}
You can also create a character extension as follow:
extension Character {
var integerValue: Int? {
return Int(String(self))
}
}
Testing
Character("1").integerValue // 1
Character("2").integerValue // 2
Character("3").integerValue // 3
Character("4").integerValue // 4
Character("5").integerValue // 5
Character("6").integerValue // 6
Character("7").integerValue // 7
Character("8").integerValue // 8
Character("9").integerValue // 9
Character("0").integerValue // 0
Character("a").integerValue // nil
Array("9876").first!.integerValue // 9
Array("9876")[1].integerValue // 8
Array("9876")[2].integerValue // 7
Array("9876").last!.integerValue // 6
edit/update Swift 5
Swift 5 adds many new properties to the Character and one of them fits exactly to this purpose. It is called wholeNumberValue
Character("1").wholeNumberValue // 1
Character("2").wholeNumberValue // 2
Character("3").wholeNumberValue // 3
Character("4").wholeNumberValue // 4
Character("④").wholeNumberValue // 4
Character("5").wholeNumberValue // 5
Character("6").wholeNumberValue // 6
Character("7").wholeNumberValue // 7
Character("8").wholeNumberValue // 8
Character("9").wholeNumberValue // 9
Character("0").wholeNumberValue // 0
Character("万").wholeNumberValue // 10_000
Character("a").wholeNumberValue // nil
There is no need to work with characters, but your code to create an
array with the decimal digits of the input number can be greatly
simplified:
var checkSumArray = [Int]()
var tmp = number
while tmp > 0 {
checkSumArray.append(tmp % 10)
tmp /= 10
}
checkSumArray = checkSumArray.reverse()

Generate a Swift array of nonrepeating random numbers

I'd like to generate multiple different random numbers in Swift. Here is the procedure.
Set up an empty array
Generate a random number
Check if the array is empty
a. If the array is empty, insert the random number
b. If the array is not empty, compare the random number to the numbers in array
i. If the numbers are the same, repeat 2
ii. if the numbers are not the same, insert the random number and repeat 2
import UIKit
//the random number generator
func randomInt(min: Int, max:Int) -> Int {
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
}
var temp = [Int]()
for var i = 0; i<4; i++ {
var randomNumber = randomInt(1, 5)
if temp.isEmpty{
temp.append(randomNumber)
} else {
//I don't know how to continue...
}
}
If you use your method the problem is, that you will create a new random-number each time. So you possibly could have the same random-number 4 times and so your array will only have one element.
So, if you just want to have an array of numbers from within a specific range of numbers (for example 0-100), in a random order, you can first fill an array with numbers in 'normal' order. For example with for loop etc:
var min = 1
var max = 5
for var i = min; i<= max; i++ {
temp.append(i)
}
After that, you can use a shuffle method to shuffle all elements of the array with the shuffle method from this answer:
func shuffle<C: MutableCollectionType where C.Index == Int>(var list: C) -> C {
let count = countElements(list)
for i in 0..<(count - 1) {
let j = Int(arc4random_uniform(UInt32(count - i))) + i
swap(&list[i], &list[j])
}
return list
}
Ater that you can do something like that:
shuffle(temp) // e.g., [3, 1, 2, 4, 5]
The construct you’re looking for with your approach might be something like:
var temp: [Int] = []
while temp.count < 4 {
var randomNumber: Int
do {
randomNumber = randomInt(1, 5)
} while contains(temp, randomNumber)
temp.append(randomNumber)
}
This will be fine for tiny ranges like yours, but for larger ranges it will be very slow, because for the last few numbers you are waiting for the random number to hit precisely the remaining handful of possibilities. I just tried generating from a range of 200 in a playground and it took 9 seconds.
If you want a random selection of numbers with guaranteed coverage over a range, you could generate it like by taking that range and shuffling it, like this:
func shuffle<S: SequenceType>(source: S) -> [S.Generator.Element] {
var rangen = GeneratorOf { arc4random() }
let a = Array(Zip2(rangen, source))
return a.sorted { $0.0 < $1.0 }.map { $0.1 }
}
let min = 1, max = 5
shuffle(min...max)
If you want a selection of n non-repeating random numbers from a range 0..<m, there’s a particularly pretty algorithm to do this that generates an ascending sequence of random numbers from that range:
func randomGeneratorOf(#n: Int, #from: Int) -> GeneratorOf<Int> {
var select = UInt32(n)
var remaining = UInt32(from)
var i = 0
return GeneratorOf {
while i < from {
if arc4random_uniform(remaining) < select {
--select
--remaining
return i++
}
else {
--remaining
++i
}
}
return nil
}
}
Which you could use like so:
let engines = [
"Duck","Emily","Gordon","Henry", "Mavis",
"Belle","James","Edward","Thomas","Toby"
]
let picks = Array(randomGeneratorOf(n: 3, from: engines.count))
for engine in PermutationGenerator(elements: engines, indices: picks) {
println(engine)
}
Below is my suggestion.
I like this way since it is short and simple :)
let totalCount: Int = 150 //Any number you asssign
var randomNumArray: [Int] = []
var i = 0
while randomNumArray.count < totalCount {
i++
let rand = Int(arc4random_uniform(UInt32(totalCount)))
for(var ii = 0; ii < totalCount; ii++){
if randomNumArray.contains(rand){
print("do nothing")
} else {
randomNumArray.append(rand)
}
}
}

Resources