$Global={}
$Global.平均分布=`
function average(money,per)
{
    if(money < per) return false
    let aver=Math.floor(money/per)

    let arr=new Array(per)
    arr.fill(aver)
    let mod=money%per
    for (let index = 0; index < mod; index++) 
    {
        arr[index]+=1
    }
    return arr
}
return average

`

$Global.随机分布=`
function rand(money,per)
{
    if(money<per)return false

    let arr=new Array(per)
    for (let index = 0; index < arr.length; index++) 
    {
        let curMoney=money-(per-1)
        let cur=Math.ceil (Math.random()*curMoney)
        money-=cur
        per-=1
        arr[index]=cur
    }
    arr[arr.length-1]+=money
    return arr
}

return rand

`

$Global.带限制的随机分布=`
function randLimit(money,per,limit=0.5)
{
    if(money<per)return false

    let arr=new Array(per)
    for (let index = 0; index < arr.length; index++) 
    {
        let curMoney=money-(per-1)
        curMoney=curMoney*limit
        let cur=Math.ceil (curMoney)
        money-=cur
        per-=1
        arr[index]=cur
    }
    arr[arr.length-1]+=money
    return arr
}

return randLimit

`


$Global.百分比随机分布=`
function rand(lowerValue,upperValue)
{
    return Math.floor(Math.random() * (upperValue - lowerValue + 1) + lowerValue);
}

function randScale(money,per)
{
    if(money<per)return false

    let arr=new Array(per)
    let sum=0
    for (let index = 0; index < arr.length; index++) 
    {
        arr[index]=rand(1,per)
        sum+=arr[index]
    }

    let sumAll=0
    for (let index = 0; index < arr.length; index++) 
    {
        arr[index]=Math.floor(money*(arr[index]/sum))
        arr[index]=arr[index]?arr[index]:1
        sumAll+=arr[index]
    }
    arr[arr.length-1]+=(money-sumAll)
    return arr
}

return randScale

`


$Global.正太分布=`
function genSho(mean=0.5,std_dev=0.2)
{
    return randomNormalDistribution()

    function randomNormalDistribution()
    {
        var u=0.0, v=0.0, w=0.0, c=0.0;
        do
        {
            u=Math.random()*2-1.0;
            v=Math.random()*2-1.0;
            w=u*u+v*v;
        }while(w==0.0||w>=1.0)
        c=Math.sqrt(-1*Math.log(w)/w);

        let back=u*c+1
        return (back>0)?back:1;
    }
}


function randSho(money,per)
{
    if(money<per)return false

    let arr=new Array(per)
    let sum=0
    for (let index = 0; index < arr.length; index++) 
    {
        arr[index]=genSho(1,per)
        sum+=arr[index]
    }

    let sumAll=0
    for (let index = 0; index < arr.length; index++) 
    {
        arr[index]=Math.floor(money*(arr[index]/sum))
        arr[index]=arr[index]?arr[index]:1
        sumAll+=arr[index]
    }
    arr[arr.length-1]+=(money-sumAll)
    return arr
}

return randSho

`

$Global.自带随机数=
`
function rand()
{
    return Math.random()
}

return rand

`

$Global.缓存={}//用户缓存函数

$Global.和=(arr)=>
{
    if(!arr)return 0
    let sum=0
    for (let index = 0; index < arr.length; index++) 
    {
        sum+=arr[index]
    }
    return sum
}

$Global.生成数据=(arr,name)=>
{
    let obj={total:0,list:[]}
    let total=$Global.和(arr)
    if(!arr)return obj

    let list=obj.list
    for (let index = 0; index < arr.length; index++) 
    {
        let cur=arr[index]
        list.push
        (
            {
                money:cur,
                type:name,
                time:$Time.format('y/m/d h-i-s')
            }
        )
    }
    obj.total=total
    return obj
}

$Global.共用=(fun,count=10,totalCount=300)=>
{
    let pre=1/count
    let arr=new Array(count)
    arr.fill(0)

    for (let index = 0; index < totalCount*count; index++) 
    {
        let rand=fun()
        if(rand<0)continue
        for (let index = 0; index < count; index++) 
        {
            let cur=pre*(index+1)
            if(rand<=cur)
            {
                arr[index]+=1
                break
            }
        }
    }
    
    return arr
}