拆分随机值分为四是总结一下吧 [英] Split a random value into four that sum up to it

查看:124
本文介绍了拆分随机值分为四是总结一下吧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个值,如24,和我有四个文本框。如何动态生成四个值,加起来就是24?

所有值必须是整数,并不能为负,其结果不能6,6,6,6;他们必须是不同的,如:8,2,10,4。(但5,6,6,7会好起来的)

解决方案

  FUNC getRandomValues​​(amountOfValues​​:智力,总金额为:int) - > [INT] {?
    如果amountOfValues​​< 1 {
        回零
    }

    如果总金额< 1 {
        回零
    }

    如果总金额< amountOfValues​​ {
        回零
    }

    VAR值:[INT] = []
    VAR valueLeft =总金额

    因为我在0 ..< amountOfValues​​ {

        如果我== amountOfValues​​  -  1 {
            values​​.append(valueLeft)
            打破
        }
       变种值= INT(arc4random_uniform(UInt32的(valueLeft  - (amountOfValues​​  - ⅰ)))+ 1)
        valueLeft  -  =价值
        values​​.append(值)
    }

    VAR shuffledArray:[INT] = []

    因为我在0 ..< values​​.count {
        VAR RND = INT(arc4random_uniform(UInt32的(values​​.count)))
        shuffledArray.append(值[RND])
        values​​.removeAtIndex(RND)
    }

    返回shuffledArray
}

getRandomValues​​(4,24)
 

这是不是一个最终的答案,但它应该是一个(好)的起点。

它是如何工作的:它需要2个参数。随机值的数量(4你的情况)和(在你的案件24)的总金额。

有需要的总金额和0之间的随机值,将其存储在数组并减去这个从其中存储被留下的量,并存储新的值。变量

比它需要被左和0的量之间一个新的随机值,将其存储在一个阵列,它再次减去这个从所留下的量,并存储新的值。

当它是需要的最后一个数字,它看到什么量被左和补充说到阵列

编辑:

添加 +1 为随机值去除了在阵列中的问题 0

编辑2:

洗牌阵列未删除具有高值作为第一个值的机会增加。

I have one value like 24, and I have four textboxes. How can I dynamically generate four values that add up to 24?

All the values must be integers and can't be negative, and the result cannot be 6, 6, 6, 6; they must be different like: 8, 2, 10, 4. (But 5, 6, 6, 7 would be okay.)

解决方案

func getRandomValues(amountOfValues:Int, totalAmount:Int) -> [Int]?{
    if amountOfValues < 1{
        return nil
    }

    if totalAmount < 1{
        return nil
    }

    if totalAmount < amountOfValues{
        return nil
    }

    var values:[Int] = []
    var valueLeft = totalAmount

    for i in 0..<amountOfValues{

        if i == amountOfValues - 1{
            values.append(valueLeft)
            break
        }
       var value = Int(arc4random_uniform(UInt32(valueLeft - (amountOfValues - i))) + 1)
        valueLeft -= value
        values.append(value)
    }

    var shuffledArray:[Int] = []

    for i in 0..<values.count {
        var rnd = Int(arc4random_uniform(UInt32(values.count)))
        shuffledArray.append(values[rnd])
        values.removeAtIndex(rnd)
    }

    return shuffledArray
}

getRandomValues(4, 24)

This is not a final answer, but it should be a (good) starting point.

How it works: It takes 2 parameters. The amount of random values (4 in your case) and the total amount (24 in your case).

It takes a random value between the total Amount and 0, stores this in an array and it subtracts this from a variable which stores the amount that is left and stores the new value.

Than it takes a new random value between the amount that is left and 0, stores this in an array and it again subtracts this from the amount that is left and stores the new value.

When it is the last number needed, it sees what amount is left and adds that to the array

EDIT:

Adding a +1 to the random value removes the problem of having 0 in your array.

EDIT 2:

Shuffling the array does remove the increased chance of having a high value as the first value.

这篇关于拆分随机值分为四是总结一下吧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆