const obj = {
arr0: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10'],
arr1: ['11', '12', '13', '14', '15', '16', '17', '18', '19', '20'],
arr2: ['21', '22', '23', '24', '25', '26', '27', '28', '29', '30'],
arr3: ['31', '32', '33', '34', '35', '36', '37', '38', '39', '40'],
arr4: ['41', '42', '43', '44', '45', '46', '47', '48', '49', '50'],
arr5: ['51', '52', '53', '54', '55', '56', '57', '58', '59', '60'],
arr6: ['61', '62', '63', '64', '65', '66', '67', '68', '69', '70'],
arr7: ['71', '72', '73', '74', '75', '76', '77', '78', '79', '80'],
}
// 洗牌算法,打散数组元素
class Solution {
constructor(nums) {
this.nums = nums;
this.original = this.nums.slice();
}
// 重置
reset() {
this.nums = this.original.slice();
return this.nums;
}
// 清洗
shuffle() {
for (let i = 0; i < this.nums.length; ++i) {
const j = Math.floor(Math.random() * (this.nums.length - i)) + i;
const temp = this.nums[i];
this.nums[i] = this.nums[j];
this.nums[j] = temp;
}
return this.nums;
}
}
// 数组总和
function baseSum(array) {
var result,
index = -1,
length = array.length;
while (++index < length) {
var current = parseInt(array[index]);
if (current !== undefined) {
result = result === undefined ? current : (result + current);
}
}
return result;
}
// 获取当天的21:30:00时间戳
function getNowFormatDate() {
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = '0' + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = '0' + strDate;
}
const time = year + '-' + month + '-' + strDate + ' 21:30:00'
return new Date(time).getTime();
}
// 获取随机下标 时间戳除以数组的和,直到位于0-9之间
function getIndex(time, total) {
let index = Math.round(time / total)
if (index >= 0 && index <= 9) {
return index
} else {
return getIndex(index, total)
}
}
// 获取基础的8个数据
function getBase8Arr() {
let newArr = []
let time = getNowFormatDate()
for (let i = 0; i <= 7; i++) {
let arr = obj[`arr${i}`]
let solution = new Solution(arr)
let sortArr = solution.shuffle()
const index = getIndex(getNowFormatDate(), baseSum(arr))
newArr.push(sortArr[index])
}
return newArr
}
// 获取剩余2个的唯一摇号
function getItem(baseArr = []) {
let num = Math.floor(Math.random() * 80 + 1)
if (num >= 1 && num <= 9) {
num = '0' + num;
}
num = String(num)
if (baseArr.includes(num)) {
return getItem(baseArr)
} else {
return num
}
}
// 最终得到的结果
function getLastArr() {
const baseArr = getBase8Arr()
for (let i = 0; i < 2; i++) {
let item = getItem(baseArr)
baseArr.push(item)
}
return baseArr.sort()
}
const totalArr = getLastArr()
console.log(totalArr)
console