0

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--;
}

}

2 Answers 2

2

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

someArray.sort( function() { return (Math.random() - 0.5); } );
Sign up to request clarification or add additional context in comments.

Comments

1

I have included the areas of your code that I either modified or where I added logic.

function randomOrderArraySort(){
  return Math.round( Math.random() )-0.5;
}

var imageListArray = new Array();

for ( var i = minval; i <= maxval; i++ )
{
    if (Math.random() < ( select / remaining))
    {
        imageListArray.push('/Users/poe/images/physics304/Principle_' + pad(i, 5) + '.jpg');

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

imageListArray.sort( randomOrderArraySort ); // Randomize Sorting.

document.form.out.value = "";
for (var i = 0; i < imageListArray.length; i++) {
    document.form.out.value = document.form.out.value +
        imageListArray[i] + '\n';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.