From 77b70442eaa17a95f78ebdf8d1737721907cff1a Mon Sep 17 00:00:00 2001 From: TienNM Date: Mon, 8 Feb 2021 20:30:54 +0700 Subject: [PATCH] [Change] random number algorithm --- web/.gitignore | 1 + web/script.js | 81 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 web/.gitignore diff --git a/web/.gitignore b/web/.gitignore new file mode 100644 index 0000000..1d74e21 --- /dev/null +++ b/web/.gitignore @@ -0,0 +1 @@ +.vscode/ diff --git a/web/script.js b/web/script.js index 3e6eea6..1d15dbb 100644 --- a/web/script.js +++ b/web/script.js @@ -1,16 +1,61 @@ +let numInCol = [ + [1, 2, 3, 4, 5, 6, 7, 8, 9], + [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], + [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], + [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], + [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], + [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], + [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], + [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], + [80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90] +]; + let numRows = 9; let numCols = 9; let numPerRow = 5; +let maxNumPerCol = 6; let lotoKey = "loto_"; -function randomNumbers(num, from, to) { - let arr = Array.from({ - length: to - from - }, (_, i) => from + i); - arr.sort(() => 0.5 - Math.random()); - return arr.slice(0, num).sort(); +function randomANumberInRow(weights) { + let tempWeight = [...weights]; + for (let i = 1; i < tempWeight.length; i++) { + tempWeight[i] += tempWeight[i - 1]; + } + let rand = Math.floor(Math.random() * tempWeight[tempWeight.length - 1]); + for (let i = 0; i < tempWeight.length; i++) { + if (rand < tempWeight[i]) return i; + } } +function randomARow(baseWeight) { + console.log("Before", JSON.stringify(baseWeight)); + let tempWeight = [...baseWeight]; + let selectedNum = []; + for (let i = 0; i < numPerRow; i++) { + let num = randomANumberInRow(tempWeight); + selectedNum.push(num); + tempWeight[num] = 0; + baseWeight[num]--; + } + console.log("After", JSON.stringify(baseWeight)); + return selectedNum; +} + +function randomNumbersInCol(num, col) { + if (col < 0 || col >= numInCol.length) return; + let arr = numInCol[col]; + arr.sort(() => 0.5 - Math.random()); + return arr.slice(0, num); +} + +// function randomSelectCols() { +// let arr = Array.from({ +// length: 9 +// }, (_, i) => i); +// arr.sort(() => 0.5 - Math.random()); +// return arr; +// } + function generate() { let node = document.getElementById("grid"); if (node.innerHTML && !confirm("Bạn có muốn tạo lại bảng không?")) @@ -18,17 +63,31 @@ function generate() { let cell = new Array(numRows).fill(0).map(() => new Array(numCols).fill(0)); let countNumPerCol = new Array(numCols).fill(0); - //Random cac cot co so trong tung dong + // //Random cac cot co so trong tung dong + // for (let i = 0; i < numRows; i++) { + // let selectedCol = randomSelectCols(numPerRow, 0, 9); + // let count = 0; + // for (let j = 0; j < selectedCol.length; j++) { + // let col = selectedCol[j]; + // if (countNumPerCol[col] == maxNumPerCol) continue; + // countNumPerCol[col] += 1; + // cell[i][col] = -1; + // count++; + // if (count == numPerRow) break; + // } + // } + let baseWeight = new Array(numCols).fill(6); + for (let i = 0; i < numRows; i++) { - let selectedCol = randomNumbers(numPerRow, 0, 9); - selectedCol.forEach(col => { - countNumPerCol[col] += 1; + let newRow = randomARow(baseWeight); + newRow.forEach(col => { + countNumPerCol[col]++; cell[i][col] = -1; }); } for (let i = 0; i < numCols; i++) { - let selectedNum = randomNumbers(countNumPerCol[i], 10 * i + 1, 10 * (i + 1) + 1); + let selectedNum = randomNumbersInCol(countNumPerCol[i], i); for (let j = 0; j < numRows; j++) { if (cell[j][i] == -1) { cell[j][i] = selectedNum.shift();