mercredi 22 avril 2020

Incorrect calculation of random number in Vue?

I really don't understand this. This is in a Vue method, with my variables being declared in the data object:

console.log('dice_max: ' + this.dice_max);   // dice_max: 6
console.log('dice_min: ' + this.dice_min);   // dice_min: 5
this.dice = Math.floor(Math.random() * (this.dice_max - this.dice_min + 1) + this.dice_min);
console.log('dice: ' + this.dice); // dice: [either 1 or 0] -> I'm expecting 5 or 6

Any ideas?

I don't know if my watches have something to do with it:

watch: {
        dice_max: function (val) {
            if (val < this.dice_min) {
                this.dice_max = this.dice_max_last;
            } else {
                this.dice_max = val;
                this.dice_max_last = this.dice_max;
            }
        },
        dice_min: function(val) {
            if (val > this.dice_max) {
                this.dice_min = this.dice_min_last;
            } else {
                this.dice_min = val;
                this.dice_min_last = this.dice_min;
            }
        }
    }



Aucun commentaire:

Enregistrer un commentaire