Full version: jsB@nk » Calculation » Array Randomiser
URL: https://www.javascriptbank.com/array-randomiser.html
This is an example of using the Array() object's own sort method to randomize itself. Very simple and clean.
Full version: jsB@nk » Calculation » Array Randomiser
URL: https://www.javascriptbank.com/array-randomiser.html
<script language="JavaScript"><!--// by Roland Levy [email protected] randomisedArray(max){counter = max ;finalArray = new Array() ;prevArray = new Array() ;for (i = 0; i < max; i ++) prevArray[i] = i + 1 ;for (j = 0; j < max; j ++){tempArray = new Array() ;rand = Math.round(Math.random() * (counter-1)) ; finalArray[j] = prevArray[rand]x = 0for (k = 0; k < counter; k ++){ if (k != rand){tempArray[x] = prevArray[k] ;x ++ ;}}counter -- ;prevArray = tempArray ;}return finalArray}result = 0while (!result){result = prompt("Random array Length : ", 10)result = ((result == "") + parseFloat(result)) ? result : false}msg = "Ordered array: "for (i = 1; i <= result; i ++) msg += i + ((i < result) ? "," : "<BR>")msg += "Random array: " + randomisedArray(result)document.write(msg) ;//--></script><!-- This script downloaded from www.JavaScriptBank.com Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->