-
javascript array rand
- This function (with demo code) will get you a random element from an array (or any object as it’s written).
<script type="text/javascript">//<![CDATA[
function randomElement ( myArray ) {
if ( ! myArray ) return false;
return myArray[ Math.floor( Math.random() * myArray.length ) ];
}
var stuff = new Array('one', 'two', 'three', 'four', 'five');
for ( i = 0 ; i < 10; i++ ) {
document.write( randomElement( stuff ) + ' ');
}
//]]>
</script>
-
See also javascript array rand [2].
-
-
[close]
Permanent link · http://querylog.com/q/javascript+array+rand
Suggested HTML for linking:
Link preview: javascript array rand
-
04 October 2006
· Internet & computing
-
The page found by the original query:
Randomize arrays in JavaScript with the Fi…
|