Array.prototype.randomWeightedByPosition ??= function(p){
    const length = this.length;
    const totalWeight = (p-p^(length+1))/(1-p);

    const randomNumber = Math.random() * totalWeight;
    for (let i = 0; i < length; i++) {
        const numberOfElement = i+1;
        const weightTilHere = (p-p^(numberOfElement+1))/(1-p);
        if(weightTilHere <= randomNumber)
            return this[i];
    }
    return this.last();
}