gpt4 book ai didi

javascript - 数组中带有权重的随机数

转载 作者:行者123 更新时间:2023-12-03 01:30:00 32 4
gpt4 key购买 nike

我希望能够创建类似于老虎机结果的随机数数组。我知道如何提取随机数,但我想对整个结果进行加权。

例如,在下面的代码中,我只想允许 row2 在 10000 次中返回 [6,6,6] 1 。

我遇到的问题是我可以创建一个不会显示 6 的函数,除非万分之一,但这不是我想要的。我想将其显示在行中,但只是不想要特定的结果,除非每 10000 次旋转一次。

我想为所有获胜盘设置不同的赔率。有什么建议吗?

这是迄今为止我的代码:

var row1 = [0, 0, 0];
var row2 = [0, 0, 0];
var row3 = [0, 0, 0];

var bar1 = 1;
var bar2 = 2;
var bar3 = 3;
var cherry = 4;
var seven = 5;
var diamond = 6;

var balance = 100;
function loadBalance(buyIn) {
return (balance += buyIn);
}

var reelArray = [1, 2, 3, 4, 5, 6];

function randomChoice(arr) {
const randIndex = Math.floor(Math.random() * arr.length);
return arr[randIndex];
}



function spin() {
for (j = 0; j <= 2; j++) {
row1[j] = randomChoice(reelArray);
row2[j] = randomChoice(reelArray);
row3[j] = randomChoice(reelArray);
}

console.log(`${row1}\n${row2}\n${row3}`);
}

function play() {
spin();
checkWinner();
}

function checkWinner() {
if (row2[0] != row2[1]) {
console.log("you lost");
balance = balance - 1;
console.log(`bal: `, balance);
return balance;
} else if (row2[1] != row2[2]) {
console.log("you lost");
balance = balance - 1;
console.log(`bal: `, balance);
return balance;
} else {
console.log("you won!");
switch (row2[0]) {
case 1:
balance = balance + 20;
console.log(`bal: `, balance);
break;
case 2:
balance = balance + 40;
console.log(`bal: `, balance);
break;
case 3:
balance = balance + 80;
console.log(`bal: `, balance);
break;
case 4:
balance = balance + 20;
console.log(`bal: `, balance);
break;
case 5:
balance = balance + 200;
console.log(`bal: `, balance);
break;
case 6:
balance = balance + 1000;
console.log(`bal: `, balance);
break;
default:
break;
}
}
return;
}

function runIt() {
for (i = 0; i <= 100; i++) {
play();
}
}

runIt();

最佳答案

要更改特定结果的赔率吗?

您可以检查这些特定分数,然后进行额外的随机检查。

下面是模拟和计算此类方法的代码片段。

它不能保证被操纵的结果每 N 次中就有 1 次被准确地选取。
但它们发生的几率会降低。

另一种方法是存储一个带有操纵结果计数器的对象。然后仅当此类操纵结果的计数器仍低于其限制时才递归。当它被允许通过时重置它的计数器。但知道使用这种方法的赌徒只能等待其他赌徒浪费赌注并愤怒退出。然后在接近该限制之前接管。从而增加了他获胜的几率。

const reelWords = ['bar1', 'bar2', 'bar3', 'cherry', 'seven', 'diamond'];
const riggedOdds = { 111: 100, 222: 200, 333: 300, 444: 400, 555: 500, 666: 10000 };
const winningcombos = [111, 222, 333, 444, 555, 666];

function getRandomReel(elementLimit) {
let N = (elementLimit < 2) ? 2 : elementLimit;
if(N > reelWords.length) N = reelWords.length;

let arrNums = [];
let arrWords = [];

while(arrNums.length < 3){
let idx = Math.floor(Math.random() * N); // random 0 to N-1
arrNums.push(idx+1);
arrWords.push(reelWords[idx]);
}

let arrCombo = Number(arrNums.join(''));
if(arrCombo in riggedOdds){
let riggedOdd = riggedOdds[arrCombo];
let newOdd = Math.floor(riggedOdd/(N*N*N));
let n = Math.floor(Math.random()*(newOdd+1));
//console.log('combo:'+ arrCombo +', rigged:'+ riggedOdd +', new odd:'+ newOdd +', n:'+ n);
if(n > 0) {
return getRandomReel(N); // recurse
}
}
return {"combo": arrCombo, "words": arrWords };
}

function printDictAsTable(dict){
let htmlstr = "<table border=1><tr><th>Number</th><th>Count</th></tr>";

let sortedKeys = Object.keys(dict).sort(function(a,b){return b-a});

for (let i=0; i<sortedKeys.length; i++) {
let key = sortedKeys[i];
htmlstr += "<tr><td>"+ key +" </td><td>" + dict[key] + " </td></tr>";
}
htmlstr += "</table>";
document.getElementById("resultContainer").innerHTML = htmlstr;
}

function loopRiggedOneArmedBanditSimulationAndPrint(){
let elementLimit = Number(document.getElementById("elementLimit").value);
let loopTotal = Number(document.getElementById("loopLimit").value);
let dict = {};

for(let i = 0; i < loopTotal; i++){
let reel = getRandomReel(elementLimit);
let combo = reel.combo;
dict[combo] = dict[combo] ? dict[combo] + 1 : 1;
}

printDictAsTable(dict);
}

function GambleAndPrint() {
let elementLimit = reelWords.length;
document.getElementById("rangeInput").value = elementLimit ;
document.getElementById("elementLimit").value = elementLimit;
let htmlstr = "<table border=1>";
for(let i = 0; i < 3; i++){
let reel = getRandomReel(elementLimit);
let arr = reel.words;
let winorlose = (winningcombos.indexOf(reel.combo) > -1) ? 'won' : '...';
htmlstr += "<tr><td>"+arr[0]+"</td><td>"+arr[1]+"</td><td>"+arr[2]+"</td><td>"+ winorlose +"</td></tr>";
}
htmlstr += "</table>";
document.getElementById("resultContainer").innerHTML = htmlstr;
}

loopRiggedOneArmedBanditSimulationAndPrint();
<div id="topContainer">
<div id="inputAndButton" style="float: left;">
<p>Elements :
<input type="number" id="elementLimit" value=2 readonly><br/>
<input type="range" id="rangeInput" min=2 max=6 value=2 onchange="document.getElementById('elementLimit').value=this.value;">
</p>
<p>Loops: <input type="number" id="loopLimit" value=10000></p>
<p><button type="button" onclick="loopRiggedOneArmedBanditSimulationAndPrint()">
Loop and Count Simulation
</button>
<button type="button" onclick="GambleAndPrint()">
Gamble
</button>
</p>
</div>
<div id="resultContainer" style="float: right;"></div>
</div>

关于javascript - 数组中带有权重的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51345563/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com