使用不同的随机数范围取决于相应元素属于哪个数组

时间:2018-04-24 14:37:35

标签: ios swift swift4

对于罗嗦的标题感到抱歉,我无法想出如何最好地说出这一点。 (如果有人对基本原则有更好的标题,请告诉我!)

所以,我已经为随机锻炼应用程序提供了2个数组'。在'生成锻炼'功能,我生成一个随机数的练习,随机挑选它们,然后为每个练习分配一些代表(再次,随机)。

我已经让它工作正常但是有一些适合大量代表的练习(俯卧撑可能适合10到30之间的arc4random)和一些适合少量代表(1英里)运行适合弧1随机1 - 5之间说。)

在创建锻炼时,我现在需要检查"位置[0]中的锻炼元素是否属于'低代表阵列'使用代表密钥X,如果它属于'高代表阵列'使用代表键Y.

我想我可以做随机数生成位但是我大量地卡在整个检查件上。

这是我的代码(伪代码更多地解释了我希望实现的目标)

let highRepsArray = ["push ups", "star jumps", "watch tv"]
let lowRepsArray = ["500m row", "3 mile run", "lift a truck"]
let finalExerciseArray = highRepsArray + lowRepsArray
//create function for number of exercises in this specific workout

func generateNewWorkout() -> (randomExerciseArray:Set<String>, randomRepsArray:[Int]) {

    let randomKey = Int(arc4random_uniform(4) + 3)
    var workoutSet = Set<String>()
    let possibleExercises = finalExerciseArray
    var repsSet = [Int]()

    while workoutSet.count < (randomKey) {
        let randomIndex = Int(arc4random_uniform(UInt32(possibleExercises.count)))
        workoutSet.insert(possibleExercises[randomIndex])
    }

//generate the reps array by counting through the same random key used to generate the number of exercises

    while repsSet.count < (randomKey) {

//here's what i can't work out!

        IF THE EXERCISE AT POSITION [0] IS IN EXERCISEARRAY1 THEN CHOOSE A RANDOM NUMBER FROM 1 - 10, IF IN EXERCISE ARRAY2 CHOOSE FROM 5 - 10 AND APPEND IT IN THAT POSITION
        IF THE EXERCISE AT POSITION [1] IS IN EXERCISEARRAY1 THEN CHOOSE A RANDOM NUMBER FROM 1 - 10, IF IN EXERCISE ARRAY2 CHOOSE FROM 5 - 10 AND APPEND IT IN THAT POSITION

//append the rep number into the array - here it's just the same range for every exercise, but I need to split this out as above
        repsSet.append(Int(arc4random_uniform(20)+10))
    }

这几乎是我需要做的最后一件事,我可以在我的应用程序完成之前进行锻炼,所以希望有人可以提供帮助! : - )

1 个答案:

答案 0 :(得分:0)

您的模型对您的需求是错误的。您需要使用包含练习名称和合适的代表范围的自定义结构。然后从该范围内生成随机数是微不足道的。