2017-10-02 4 views
0

私は8人5000リンゴを持っているといいますか?
私はすべてのリンゴをすべての8人に渡したいので、私はリンゴが残っていません。
しかし、誰もが異なる量割り切り/ランダムな額を渡す

それらすべてを与えるためには何だろう最善の方法を取得する必要がありますか?

私はこれとの開始:

let people = 8 
 
let apples = 5000 
 

 
function getRandomInt(min, max) { 
 
    return Math.floor(Math.random() * (max - min + 1)) + min 
 
} 
 

 
while (people--) { 
 
    // last person get the rest 
 
    let x = people ? getRandomInt(0, apples) : apples 
 

 
    // subtract how many apples i got left 
 
    apples -= x 
 

 
    console.log(`Giving person ${people + 1} ${x} apples (got ${apples} left)`) 
 
}

しかし、このことについて、私は好きではない事は最後の人が(5個のりんごそして時には少ない)非常に少数のリンゴを取得することで、最初の人が他の人よりも先に進む

+0

リンゴの80%を取り、それらを均等に分けて、最後の20%をランダム化することができます。味を調整する。 –

+0

あなたはまた、人数に基づいてリンゴの最低割合を設定することもできます。var minPercentage =(100/people) - 10 //または分散を希望するものは、1人あたりのリンゴが等しいか、最小パーセンテージ。それらを格納し、何かを印刷する前に小切手を実行するための配列が必要です。 – Danimal

+0

私はあなたがそれをやる方法の例を教えてくれますか? – Endless

答えて

0

は、私はそれを把握考えます。ちょっとピンツばかりでした。誰もが1000少ないし、その後100個のりんご以上を持っていないが、これがどうなるそれでも

let people = 8 
 
let apples = 5000 
 

 
function getRandomInt(min, max) { 
 
    return Math.floor(Math.random() * (max - min + 1)) + min 
 
} 
 

 
while (people--) { 
 
    // last person get the rest 
 
    let x = people ? getRandomInt(0, Math.floor(apples/people)) : apples 
 

 
    // subtract how many apples i got left 
 
    apples -= x 
 

 
    console.log(`Giving person ${people + 1} ${x} apples (got ${apples} left)`) 
 
}

let x = people ? getRandomInt(0, Math.floor(apples/people)) : apples

。それ以外の場合は、もっと複雑になります。翌日、私は2500のリンゴと6人しかいないかもしれません...

1

ランダムに「バランスのとれた」結果が毎回必要な場合は、バランスまたはランダムのいずれかに優先順位を付ける必要があります。ここにあなたの "widestGapの要件以下の一つの可能​​な解決策があります:

function randomDeltas(length, widestGap, remToGet) { 
 
    // widestGap >= length * 2 - 1 
 
    let deltas = []; 
 
    let sum = 0; 
 
    let start = 0; 
 
    let origLength = length; 
 
    while (length--) { 
 
    start += 1 + Math.floor(Math.random() * widestGap); 
 
    deltas.push(start); 
 
    sum += start; 
 
    } 
 
    
 
    let rem = sum % origLength; 
 
    let correction = remToGet - rem; 
 
    if (correction !== 0) { 
 
    sum -= deltas[0]; 
 
    deltas[0] += correction; 
 
    if (deltas[0] >= deltas[1]) { 
 
     deltas[0] -= origLength; 
 
    } 
 
    else if (deltas[0] < deltas[1] - widestGap) { 
 
     deltas[0] += origLength; 
 
    } 
 
    sum += deltas[0]; 
 
    } 
 
    return { 
 
    deltas, 
 
    sum 
 
    }; 
 
} 
 

 
function randomDistinctDistribute(apples, people) { 
 
    let rem = apples % people; 
 
    let { deltas, sum } = randomDeltas(people, people * 2 - 1, rem); 
 
    let div = (apples - sum)/people; 
 
    let distribution = []; 
 
    while (deltas.length) { 
 
    distribution.push(div + deltas.shift()); 
 
    } 
 
    return distribution; 
 
} 
 

 
console.log(randomDistinctDistribute(5000, 8)); 
 
console.log(randomDistinctDistribute(2500, 6));

ここでの考え方は、デルタをランダム化する(ギャップが大きくなったことがないことを確認するため)、その後、除数にそれらのデルタを適用しています。ここで


は異なる値とバランスの取れた分布を得るために、元の(決定論的)なアプローチです:

function distinctDividents(apples, people) { 
    let distribution = []; 
    let div = Math.floor(apples/people); 
    let rem = apples % people; 
    if (people % 2) { 
    distribution.push(div); 
    people--; 
    } 
    let half = people/2; 
    let i = 1; 
    while (i <= half) { 
    distribution.push(div - i); 
    distribution.unshift(div + i); 
    i++; 
    } 
    if (rem) { 
    distribution[0] += rem; 
    } 
    return distribution; 
} 

console.log(distinctDividents(5000, 8)); 
+0

を参照してください。問題はそれが予測可能であるということです。私はそれを実行するたびにいつも同じ結果になります。私は毎回無作為にしたい。私はまたそれが均等に分割されると思った(differensenは最大と最小の間のちょうど15だった) – Endless

+0

ランダムバランスの取れたアプローチのためのイラストでコードを更新した。あなたが望むようにparamsを変更することはできません(例えば、2500と6を試してください;あるいは2600と9!)。 – raina77ow

関連する問題