I need a simple Javascript RNG that will pick the numbers 1-5 and write them to screen. But I need it to write all 5 numbers randomly so if a number has already been chosen it would skip that number and loop again until all 5 numbers have been picked. Thanks
Sounds easy, initialize an array with values 1-5 and shuffle it. This would assure that the numbers are unique. Here is an example for array shuffling (the last function at the page, random sort): http://www.javascriptkit.com/javatutors/arraysort.shtml
How would I get this function to print the numbers in the alert box with spaces instead of commas and also how would I get it to print vertical instead of horizontal? function getaQuote() { var myarray=[0, 1, 2, 3, 4, 5] myarray.sort(function() {return 0.5 - Math.random()}) alert("Your Numbers: " + myarray) }
Shouldn't an array be: var myarray = array("1","2","3","4","5"); I'm not sure, maybe your way works too. var one = myarray[0]; var two = myarray[1]; var three = myarray[2]; var four = myarray[3]; var five = myarray[4]; document.write(one+"<br />"+two+"<br />"+three+"<br />"+four+"<br />"+five); My JavaScript - not so good. That should work.
Your script is missing the sort function, my script works as it is, but I can't figure out the formatting of the output.