1

I'm trying to generate a random 'workout' with a random number of exercises.

Each exercise needs to have a name (from an array) and a number of reps (a random number which will eventually have 'upper and lower' bounds depending on what type of exercise it is (e.g. up to 10 for push ups, but only 1 for '500m row'. But that can come later! For now, I'm trying to work out how to generate a random number of class instances (I'll then pass these via a segue and put in a tableView, assuming that's possible)

Here's my code, which is just in a playground for now :

import UIKit

let exerciseArray = ["squats", "pushups", "lunges", "jumping jacks"]

class exerciseInWorkout {

    var exerciseName : String
    var exerciseReps : Int

    init(name: String, reps: Int) {
        exerciseName = name
        exerciseReps = reps
    }

}

let randomkey = Int(arc4random_uniform(4))
let numberOfExercisesInWorkout = Int(arc4random_uniform(10))

// Manually creating a new object of exerciseInWorkout with a random exercise and a random number of reps

let exerciseOne = exerciseInWorkout(name:exerciseArray[randomkey],     reps:Int(arc4random_uniform((30))))

//print result to make sure it works

print(exerciseOne.exerciseName, exerciseOne.exerciseReps)

//Have some function here which creates a number of class instances based on the "numberOfExercisesInWorkout" constant

If I'm completely on the wrong track here feel free to tell me but I think I'm not far off (hopefully...)

1 Answer 1

3

There are probably a lot of different (valid) approaches to this. One of which could be something like this:

let exerciseArray = ["squats", "pushups", "lunges", "jumping jacks"]

struct Exercise {
    let name: String
    let reps: Int
}

func randomInt(upperBound: UInt32) -> Int {
    return Int(arc4random_uniform(upperBound))
}

let numberOfExercisesInWorkout = randomInt(upperBound: 10)

let exercises: [Exercise] = (1...numberOfExercisesInWorkout).map { _ in
    let randomKey = randomInt(upperBound: UInt32(exerciseArray.count))
    return Exercise(name: exerciseArray[randomKey], reps: randomInt(upperBound: 10))
}

print(exercises)

Which ouputs something like this:

[Exercise(name: "lunges", reps: 8), Exercise(name: "jumping jacks", reps: 6), Exercise(name: "lunges", reps: 4), Exercise(name: "squats", reps: 9), Exercise(name: "jumping jacks", reps: 5), Exercise(name: "squats", reps: 9)]


Note: You could also modify the (randomInt) helper function to take into account a lowerBound I guess

Sign up to request clarification or add additional context in comments.

6 Comments

this is great thank you - exactly what i was looking for and see your point on the lower bounds. When I print this though I get [__lldb_expr_241.Exercise(name: "squats", reps: 2) the lidb_exp... is repeated at the start of each instance - do you know what causes that?
also - is there a way to add a number to each instance i.e. to identify each exercise for then putting in a table view? e.g. "Exercise1" - or would there be no need to do that for a table view?
It's the name of the module that Exercise is member of (I guess). Are you running this as an expression in the debugger maybe?
@nc14 If you're looking for a simple s/n then you can use the index of the instance in the list.
yes sorry that makes perfect sense - thanks so much! Can't work out that [__lldb_expr_243 thing at the moment - it's in Playground yes but I don't know if I'm running it as an 'expression' i'm just printing in the playground - any ideas? Saw here... stackoverflow.com/questions/31020725/… but mine aren't empty so not sure?
|

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.