javascript - 我在哪里失去随机性?
问题描述
所以我正在制作一个石头、纸、剪刀游戏。我想让 math.random() 给我一个随机结果,我将其与用户选择的结果进行比较。
它主要工作。我什至认为它在一段时间内完全有效,但我在某些时候失去了随机性。
为了尝试它,我输入了一个固定的“用户”选项并使用 setInterval(myfunction, 20) 运行代码几百次。他们总是不平衡的胜利,总是有相同的结果:
如果我用 playerPick = 1 运行它,计算机总是赢。
如果我用 playerPick = 2 或 3 运行它,用户总是赢。
任何人都可以看到我犯错的地方吗?
//Global variable and constants.
const ROCK = 1;
const PAPER = 2;
const SCISSORS = 3;
//This is the game.
function whoWins(){
const playerPick = 2; //for debugging, it can be 1, 2, or 3.
const computer = computerPick();
if (playerPick == computer){
return draw();
} else if (playerPick == 1 && computer == 2){
return lose();
} else if (playerPick == 2 && computer == 3){
return lose();
} else if (playerPick == 3 && computer == 1){
return lose();
} else {
return win();
}
}
//These are the inputs for the game.
rockButton.addEventListener('click', () => {
playerPick = ROCK;
return whoWins()});
paperButton.addEventListener('click', () => {
playerPick = PAPER;
return whoWins()});
scissorsButton.addEventListener('click', () => {
playerPick = SCISSORS;
return whoWins()});
function computerPick() {
let computerChoice = '';
const getRandom = Math.random;
if (getRandom() >= 2/3) {
computerChoice = ROCK;
} else if (getRandom() >= 1/3){
computerChoice = PAPER;
} else {
computerChoice = SCISSORS;
}
return computerChoice;
}
我对这一切都很陌生,但这仍然不是随机的。
解决方案
这应该是一个简单的修复,如您需要调用 Math.random 一次的评论中所述,否则概率会出现偏差。
我认为使用原始代码的 PAPER 的概率应该是 0.66 * 0.66 = ~ 44%,而 SCISSORS 的概率应该是 0.66 * 0.33 = ~ 22%。新功能应该可以解决这个问题。
const ROCK = 1;
const PAPER = 2;
const SCISSORS = 3;
// Original computerPick function
function computerPickOriginal() {
let computerChoice = '';
const getRandom = Math.random;
if (getRandom() >= 2/3) {
computerChoice = ROCK;
} else if (getRandom() >= 1/3){
computerChoice = PAPER;
} else {
computerChoice = SCISSORS;
}
return computerChoice;
}
// Fixed computerPick function.
function computerPick() {
let computerChoice = '';
const choice = Math.random();
if (choice >= 2/3) {
computerChoice = ROCK;
} else if (choice >= 1/3){
computerChoice = PAPER;
} else {
computerChoice = SCISSORS;
}
return computerChoice;
}
function decodeChoice(choice) {
if (choice == ROCK) return "Rock";
if (choice == PAPER) return "Paper";
if (choice == SCISSORS) return "Scissors";
}
// Check the distribution of each version of the code.
console.log("Checking distributions (10000 picks)..");
let original_counts = {};
let counts = {};
for(let i = 0; i < 10000; i++) {
let k = computerPick();
counts[k] = (counts[k] || 0) + 1;
let k2 = computerPickOriginal();
original_counts[k2] = (original_counts[k2] || 0) + 1;
}
console.log('Computer Pick Distribution (original): ', Object.entries(original_counts).map(([key,value]) => `${decodeChoice(key)}: ${value}`));
console.log('Computer Pick Distribution (fixed): ', Object.entries(counts).map(([key,value]) => `${decodeChoice(key)}: ${value}`));
推荐阅读
- javascript - 过滤数组中的对象中的元素
- reactjs - 将父应用程序(角度)中的 css 排除在单水疗应用程序中的子应用程序(反应)中
- python - 当进程正在写入文件时,“kill -INT pid”和“kill pid”之间的区别
- python - “str”对象在 discord.py 问题中没有属性“name”
- nearprotocol - 如何查询余额历史
- javascript - AsyncStorage getItem 方法调用缓慢
- javascript - 反应:输入值未在表单中定义
- asp.net - DotNet 5.0 Swagger 未在 Azure 应用服务中加载
- html - 如何使用 CSS 代码制作带有 Scroll 和固定 Header 的表格,在任何情况下以及在手机上看起来都不错?
- keras - 神经机器翻译 - seq2seq 编码器-解码器