mercredi 15 mars 2023

Math.random is messing with probablity?

So i wrote this code while tinkering around with async stuff and stumbled on a puzzle. I know i'm just missing something or what ever, but i just want to talk this out and see if my reasoning is sound!

I made a game, basically producing a number from 1-4, and trying to get a 3 or less (represented by the timeout), 7 times in a row (represented by 7 bgColor changes to make it do something).

Here is where the probabilty comes in: probablity of getting 3 or less out of four = 3/4 or .75, 75% whatever you like.

The probablity of that happening 7 times in a row is (3/4)^7 or .13348

I have run this code, as it stands, with a 200 sample rate. Initially, the ratio stays around .13 kinda for about 4 "wins" and then shoots up to leaving off at .26 almost EXACTLY 2(.133)! I'm wondering somewhere in the Math.random code I could find this? Does anyone have any direction for me to think about? This is my first post to SO! Thanks in advance!

const delayColor = (color) => {
    let delay = Math.floor(Math.random()*5)*10;
    return new Promise ((resolve, reject) => {
        if(delay <= 30){
            setTimeout(()=>{
                document.body.style.backgroundColor = color;
                console.log(delay)
                resolve(`${color}`)
                }, delay)}
        else {
            setTimeout(()=>{
                document.body.style.backgroundColor = 'black';
                console.log(delay)
                reject(`${color}`)
            }, delay)
        }
    })
}

let success = 0;
let total = 0;
let restarts = 0;
let wins = 0;

function clearRainbow(){
    success = 0;
    total = 0;
}

async function rainbow(){
    try {
        await delayColor('red');
        success++;
        total++;
        await delayColor('orange');
        success++;
        total++;
        await delayColor('yellow');
        success++;
        total++;
        await delayColor('green');
        success++;
        total++;
        await delayColor('blue');
        success++;
        total++;
        await delayColor('indigo');
        success++;
        total++;
        await delayColor('violet');
        success++;
        total++;
        console.log('7 In a ROW!');
        wins++;
        console.log(`Your ratio: ${success} / ${total} (${success/total}) requiring ${restarts} for a total ratio of: ${wins/restarts}`)
        console.log(`wins: ${wins}, Restarts: ${restarts}`)
        clearRainbow();
        if (wins<=200){
            rainbow();
        }
        else {
            clearRainbow();
        }
    }
    catch (e){
        console.log('Timed Out, Starting Again!')
        total++;
        restarts++;
        rainbow()
    }

    
}

I tried to run it a bunch and was expecting the "total ratio" or the overall probability of getting 3 or less out of 4, 7 times in a row to be the calculated value of .13 and am getting consistent .26 from Math.random()...

I would like to know about it and don't know where to look.




Aucun commentaire:

Enregistrer un commentaire