0

I am working on a function where I must create an array of random boolean values and I am lost at how to make this work inside a function. Any help on this would be appreciated. Sadly, I am confined to rules as to names and how I must do this so what you see is the general structure that I must use, but I can't see how logically this isn't woking.

func thirdFunction() {
//var numOfTrue = 0
//var numOfFalse = 0
var someTrue = true

var randBools = [Bool] ()

for num in 1...10{
    let random = arc4random_uniform(2)
    print(random)
    if (random == 0){
        someTrue = false
        randBools.append(someTrue)
    } else {
        randBools.append(someTrue)
    }
}
print (randBools)
}
7
  • change it to let someBool = arc4random_uniform(2) == 1 and append someBool Commented Feb 7, 2016 at 23:42
  • Your code does not work because arc4random_uniform(2) returns a number between 0 and <2. For example 0.1, 1.45, 1.999. Your code checks to see if it's exactly 0.0000. Which is VERY unlikely and not very random. However, should it ever happen, it also sets someTrue to false, so all subsequent randBools will also be false. Commented Feb 7, 2016 at 23:50
  • Since I am working with just Ints and need a 0 and 1, I still need to do this? Commented Feb 8, 2016 at 0:44
  • If you need ones and zeroes, you can use the one provided in my answer. But in your question it shows that you want an array of Bool and now you're saying you need to work with 0 and 1? Kinda confusing :). Commented Feb 8, 2016 at 0:50
  • I am sorry for the confusion. So by the guidance I need to have a loop that randomly produces a 0 or 1 from there I convert 0s into false and 1s into true (Booleans) and uses the booleans to populate the array. Commented Feb 8, 2016 at 1:04

3 Answers 3

4

This works:

var array: [Bool] = []

func randomizer() {
    var bool: Bool = false

    for num in 1...10 {
        let random = arc4random_uniform(2)
        if random == 0 {
            bool = false
        } else {
            bool = true
        }
        array.append(bool)
    }
    print(array)
}
Sign up to request clarification or add additional context in comments.

Comments

2

You set someTrue = false when the random number is zero, but you don't set it to true when the random number is one. So after the first zero, you will put false in the array on every subsequent iteration, regardless of the random number.

2 Comments

Since I am using a if-else statement and I default the variable to TRUE, I still need to do this?
You set the variable to true once, before the loop starts. Do you think that statement runs each time through the loop? It isn't inside the loop body.
0
let result = Array(1...10).map { _ in arc4random_uniform(2) == 1 }
print(result) // [false, true, true, false, true, true, true, false, false, true]

Not sure if this is what you want. (thanks to Leo for correcting my mistake)


According to your comments:

// Create an array of 1 / 0
let array = Array(1...10).map { _ in arc4random_uniform(2) }

// Convert the array to Bool
randBools = tempArray.map { Bool(Int($0)) }

Output:

print(randBools) // [false, false, true, true, false, false, true, true, true, true]

3 Comments

Yea I just noticed I forgot he wanted a Bool... :p
I don't quite understand what the == 1 part is saying.I see it isn't really implied that I am looking to get only either a 0 or 1. Maybe that clears somethings up.
the == 1 makes it return a Bool. If the random number is equal to 1, then it will return true and if it isn't then it will return false. I'd say test it out in playground if you don't understand things. Most of the answers can be tested in playground.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.