JSFiddle - React, Tailwind, and code Playground

JavaScript

function randomize(array, total){
    var maxLoops = 100, randomized;
    do{
        randomized = array.map(function(value){
           return Math.floor(Math.random()*(value+1)); 
        });
        maxLoops--;
    }while(maxLoops > 0 && randomized.reduce(function(a, b){ return a+b; }) !== total);
    
    return randomized;
}

var test = randomize([10, 5, 0, 7], 8);
console.log(test);