| | |
how do i fill random number?
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 227
Reputation:
Solved Threads: 0
say i have:
document.writeln("0")
document.writeln("1")
document.writeln("2")
document.writeln("3")
document.writeln("4")
how i do generate them in different order each time but all of them must display? ... please help
ex: .... 1
........ 3
...........0
...........4
........... 2
then another output would be:
........3
.........2
.........4.
.........1
............0 ... all random but must be all five line displaying..
thanks if anyone can help this out real quick
document.writeln("0")
document.writeln("1")
document.writeln("2")
document.writeln("3")
document.writeln("4")
how i do generate them in different order each time but all of them must display? ... please help
ex: .... 1
........ 3
...........0
...........4
........... 2
then another output would be:
........3
.........2
.........4.
.........1
............0 ... all random but must be all five line displaying..
thanks if anyone can help this out real quick
You will need to randomly generate numbers and check that they have not already been displayed. The below does this by continuosly looping until it has found the total range of values. Each loop generates a new number then checks if it exists in the array. If not the new value is appended and output to the screen.
javascript Syntax (Toggle Plain Text)
var arrayContains = function(array, value) { for(var i in array) { if(array[i] == value) { return true; } } return false; }; var found = []; var range = 4; // 0 - 4 while(found.length < range) { var num = Math.floor(Math.random() * (range + 1)); // If it is new then add it and print it if(!arrayContains(found, num)) { found[found.length] = num; document.writeln(num); } }
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
You could also try this simple variation of using arrays instead of integer values.
All codes is as follows:
hope it help...
essential
All codes is as follows:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html id="html40L" lang="en"> <head> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Style-Type" content="text/css"> <meta http-equiv="Content-Script-Type" content="text/javascript"> <meta http-equiv="Window-target" content="_top"> <title>Free Live Help!</title> <style type="text/css"> <!-- --> </style> <script type="text/javascript"> <!-- var randomNumbers = function( countUpTo ) { var sCount = 1; var count = [ ]; while( countUpTo >= sCount ) { count.push( countUpTo-- ); count.sort( function() { return .5- Math.random(); } ); } for ( var x = 0; x < count.length; x++ ) { document.writeln( String( count[ x ] ).fontcolor("green") + "<br>" ); } } // --> </script> </head> <body> <div id="output"> <script type="text/javascript"> <!-- randomNumbers( 10 ); // --> </script> </div> </body> </html>
hope it help...
essential
![]() |
Similar Threads
- Random number generator's (C++)
- Random number Sequence Generation (C++)
- Random number generation (C)
- Need to know how to create a database that will generate a random number (Database Design)
- Random number generation (C)
- Help with random number gen (C++)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Form Validation
- Next Thread: div image long link problem
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxhelp ajaxjspservlets animate automatically beta box browser bug calendar captchaformproblem cart checkbox child class close column createrange() css cursor decimal dependent design disablefirebug dom download dropdown editor element engine enter error events explorer ext file focus form forms frameworks google gwt gxt highlightedword html htmlform ie8 iframe images index internet java javascript jawascriptruntimeerror jquery jsf jsfile jump libcurl listbox math menu microsoft mimic mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent pdf php player post problem progressbar prototype regex runtime scale scroll search security select shopping size software sql text textarea w3c web website window windowofwords windowsxp wysiwyg \n





