Javascript – Randomizing output in this javascript

javascript

I would like to randomize the output of the numbers at the end of this script.
Right now it creates a list of random image names sorted by number, I would like to have the image names shuffled.

Any suggestions?

I'm new to JS and pulling my hair out over this.

Thanks!

function check(value)
{
if ( value != Math.round(value) || value <= 0 )
        alert("You must enter a positive integer in each input box.");
}

    function randint( l, u )
// Returns an integer uniformly distributed over l..u.
{
    return l + Math.floor( Math.random() * ( u + 1 - l ));
}


function generate( )
{
var select = parseInt(document.form.num.value);
if ( isNaN(select) || select != parseFloat(select) || select <= 0 )
    {
        alert("The number of random numbers desired must be a positive integer.");
        return;
}
var minval = parseInt(document.form.min.value);
if ( isNaN(minval) || minval != parseFloat(minval) || minval <= 0 )
    {
        alert("The minimum value of the range must be a positive integer.");
        return;
}
var maxval = parseInt(document.form.max.value);
if ( isNaN(maxval) || maxval != parseFloat(maxval) || maxval <= 0 )
    {
        alert("The maximum value of the range must be a positive integer.");
        return;
}


    var index = 0;
    var remaining = maxval - minval + 1;

if ( remaining <= select )
{
    alert("The number of values to select must be less than the size of the range from which to select them.");
    return;
}



function pad(number, length) {

var str = '' + number;
while (str.length < length) {
str = '0' + str;
}

 return str;

}

document.form.out.value = "";
    for ( var i = minval; i <= maxval; i++ )
{
        if (Math.random() < ( select / remaining))
    {
        document.form.out.value = document.form.out.value + '/Users/poe/images/physics304/Principle_' + pad(i, 5) + '.jpg' + '\n';

        index++;
        select--;  
        }
        remaining--;
}

}

Best Answer

To sort a list into a random order you could just do something like this:

someArray.sort( function() { return (Math.random() - 0.5); } );