samedi 14 juillet 2018

random numbers with weights in an array

I want to be able to create an array of random numbers similar to the results of a slot machine. I know how to pull random numbers but i want to weight the entire result.

So for example in the below code, i would want to only allow row2 to return [6,6,6] 1 in 10000 times.

The problem I am having is I can create a function that wont display 6 except for once in 10000 but thats not what i want. I want to display it in the rows but just dont want that specific result except once in 10000 spins.

I would want to set different odds for all the winning sets. Any advice?

here is my code so far:

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();



Aucun commentaire:

Enregistrer un commentaire