Created
March 1, 2020 14:24
-
-
Save jacky810124/dfee3b0263102e2075e5030695e4b54a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const shuffle = list => { | |
const shuffledList = [...list]; | |
for ( | |
let currentIndex = shuffledList.length - 1; | |
currentIndex > 0; | |
currentIndex-- | |
) { | |
const nextIndex = Math.floor(Math.random() * (currentIndex + 1)); | |
const nextItem = shuffledList[nextIndex]; | |
const currentItem = shuffledList[currentIndex]; | |
shuffledList[nextIndex] = currentItem; | |
shuffledList[currentIndex] = nextItem; | |
} | |
return shuffledList; | |
}; | |
const shuffle11 = list => { | |
const shuffledList = [...list]; | |
shuffledList.forEach((item, index) => { | |
const nextIndex = Math.floor(Math.random() * (list.length - index) + index); | |
const nextItem = shuffledList[nextIndex]; | |
shuffledList[nextIndex] = item; | |
shuffledList[index] = nextItem; | |
}); | |
return shuffledList; | |
}; | |
const shuffle2 = list => { | |
const shuffledList = [...list]; | |
shuffledList.forEach((item, index) => { | |
const nextIndex = Math.round(Math.random() * (list.length - 1)); | |
const nextItem = shuffledList[nextIndex]; | |
shuffledList[nextIndex] = item; | |
shuffledList[index] = nextItem; | |
}); | |
return shuffledList; | |
}; | |
const shuffle3 = list => { | |
const shuffledList = [...list]; | |
for (let i = shuffledList.length - 1; i > 0; i--) { | |
const index = Math.round(Math.random() * i); | |
const item = shuffledList[index]; | |
shuffledList.splice(index, 1); | |
shuffledList.push(item); | |
} | |
return shuffledList; | |
}; | |
const progress = value => { | |
return new Array(Math.floor(value)).fill('=').join(''); | |
}; | |
const execute = shuffleFunc => { | |
const times = 1000 * 1000 * 10; | |
const result = {}; | |
for (let i = 0; i < times; i++) { | |
const sl = shuffleFunc([1, 2, 3, 4]); | |
const k = sl.join(''); | |
if (result[k] == null) { | |
result[k] = 1; | |
} else { | |
result[k] += 1; | |
} | |
} | |
Object.keys(result).forEach((key, index) => { | |
const ratio = (result[key] * 100) / times; | |
console.log( | |
`#${index + 1} ${key} ${result[key]} ${progress(ratio)} ${ratio}` | |
); | |
}); | |
console.log(Object.values(result).reduce((prev, curr) => prev + curr, 0)); | |
}; | |
console.log('shuffle#1'); | |
execute(shuffle); | |
// execute(shuffle); | |
// execute(shuffle); | |
console.log('---------------'); | |
console.log('shuffle#11'); | |
execute(shuffle11); | |
// console.log('---------------'); | |
// console.log('shuffle#2'); | |
// execute(shuffle2); | |
// execute(shuffle2); | |
// execute(shuffle2); | |
// console.log('---------------'); | |
// console.log('shuffle#3'); | |
// execute(shuffle3); | |
// execute(shuffle3); | |
// execute(shuffle3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment