// 在連續三次拋硬幣產生相同結果(三個都是正面或三個都是反面)之前,您必須擲硬幣的最少次數是多少?可能需要的最大翻轉次數是多少?平均需要翻轉多少次?在本練習中,我們將通過創建一個模擬多個擲硬幣系列的程式來探索這些問題。
// 創建一個程式,使用亂數生成器來模擬擲硬幣幾次。模擬硬幣應該是公平的,這意味著正面的概率等于反面的概率。您的程式應該翻轉模擬硬幣,直到出現 3 個連續的正面和 3 個連續的反面。每次結果為正面時顯示 H,每次結果為反面時顯示 T,一次模擬的所有結果在同一行上。然后顯示達到 3 次連續出現相同結果所需的翻轉次數。當您的程式運行時,它應該執行 10 次模擬并報告所需的平均翻轉次數。示例輸出如下所示:
const headsOrTails = [];
const flipcounts = [];
let threeInARow = false;
while (threeInARow == false) {
const coinFlip = Math.floor(Math.random() * 10 1);
if (coinFlip <= 5) {
headsOrTails.push('H');
} else {
headsOrTails.push('T');
}
for (let index = 0; index < headsOrTails.length; index ) {
const element = headsOrTails[index];
if (element == headsOrTails[index 1]) {
if (element == headsOrTails[index 2]) {
threeInARow = true;
console.log('il numero di flip ottenuti è ' headsOrTails.length);
}
}
}
}
有沒有辦法多次重復while回圈?例如 10 次
uj5u.com熱心網友回復:
flips = [];
for (let i = 0; i < 10; i ) {
let threeInARow = false;
let results = "";
while (threeInARow == false) {
const coinFlip = Math.random();
results = coinFlip < 0.5 ? "H" : "T";
if (results.includes("HHH") || results.includes("TTT")) {
threeInARow = true;
console.log(results);
flips.push(results.length);
}
}
}
console.log(flips);
const average = arr => arr.reduce((a, b) => a b, 0) / arr.length;
console.log("Average:", average(flips))
uj5u.com熱心網友回復:
- 將 替換
while...loop為for...loop。 - 如果您的布爾變數設定為 true,則用于
break提前退出回圈。
見下文:
const headsOrTails = [];
const flipcounts = [];
let threeInARow = false;
for(let i=0; i<10; i )
{
const coinFlip = Math.floor(Math.random() * 10 1);
if (coinFlip <= 5) {
headsOrTails.push('H');
} else {
headsOrTails.push('T');
}
for (let index = 0; index < headsOrTails.length; index ) {
const element = headsOrTails[index];
if (element == headsOrTails[index 1]) {
if (element == headsOrTails[index 2]) {
console.log('the number of flips obtained is ' headsOrTails.length);
threeInARow = true;
}
}
}
if(threeInARow)
break;
}
if(!threeInARow)
{
console.log("No matches were found");
}
uj5u.com熱心網友回復:
n您可以使用幾個變數和一個滑動視窗自己跟蹤之前的翻轉有多少連續的正面或反面:
const flipCoin = () => Math.random() < 0.5 ? "H" : "T";
const flipCoinUntilNConsecutive = n => {
const flips = [], slidingWindow = [];
let windowHeadsCount = 0, windowTailsCount = 0;
while (true) {
const newestFlip = flipCoin();
flips.push(newestFlip);
slidingWindow.push(newestFlip);
newestFlip == "H" ? windowHeadsCount : windowTailsCount ;
if (slidingWindow.length == n) { // array.length is O(1).
if (windowHeadsCount == n || windowTailsCount == n) {
console.log(...flips);
return flips.length;
}
const oldestFlip = slidingWindow.shift();
oldestFlip == "H" ? windowHeadsCount-- : windowTailsCount--;
}
}
};
const n = 3, repeats = 10;
console.log(`${repeats} simulations of flipping a coin until ${n} consecutive:`);
console.log();
let minFlipsRequired = Number.MAX_VALUE, maxFlipsRequired = 0, totalFlipsRequired = 0;
for (let s = 1; s <= repeats; s ) {
const flipsRequired = flipCoinUntilNConsecutive(n);
console.log(`Simulation ${s}: ${flipsRequired}`);
minFlipsRequired = Math.min(minFlipsRequired, flipsRequired);
maxFlipsRequired = Math.max(maxFlipsRequired, flipsRequired);
totalFlipsRequired = flipsRequired;
}
console.log();
console.log(`minFlipsRequired: ${minFlipsRequired}`);
console.log(`maxFlipsRequired: ${maxFlipsRequired}`);
console.log(`totalFlipsRequired: ${totalFlipsRequired}`);
const averageFlipsRequired = totalFlipsRequired / repeats;
console.log(`averageFlipsRequired: ${averageFlipsRequired}`);
示例輸出:
10 simulations of flipping a coin until 3 consecutive:
H T H T T T
Simulation 1: 6
T T H H H
Simulation 2: 5
H H H
Simulation 3: 3
H H H
Simulation 4: 3
H T H H H
Simulation 5: 5
T H H T T H H H
Simulation 6: 8
H T H T H H H
Simulation 7: 7
H T T T
Simulation 8: 4
T H T T T
Simulation 9: 5
H H H
Simulation 10: 3
minFlipsRequired: 3
maxFlipsRequired: 8
totalFlipsRequired: 49
averageFlipsRequired: 4.9
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/474655.html
標籤:javascript 循环
