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: k2k is an unknown quantity at this point 
Solved Threads: 0
k2k k2k is offline Offline
Posting Whiz in Training

how do i fill random number?

 
0
  #1
Jun 19th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: how do i fill random number?

 
0
  #2
Jun 19th, 2009
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.

  1.  
  2. var arrayContains = function(array, value) {
  3. for(var i in array) {
  4. if(array[i] == value) { return true; }
  5. }
  6. return false;
  7. };
  8.  
  9. var found = [];
  10. var range = 4; // 0 - 4
  11. while(found.length < range) {
  12. var num = Math.floor(Math.random() * (range + 1));
  13. // If it is new then add it and print it
  14. if(!arrayContains(found, num)) {
  15. found[found.length] = num;
  16. document.writeln(num);
  17. }
  18. }
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: how do i fill random number?

 
0
  #3
Jun 19th, 2009
You could also try this simple variation of using arrays instead of integer values.

All codes is as follows:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html id="html40L" lang="en">
  4. <head>
  5. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <meta http-equiv="Content-Style-Type" content="text/css">
  8. <meta http-equiv="Content-Script-Type" content="text/javascript">
  9. <meta http-equiv="Window-target" content="_top">
  10. <title>Free Live Help!</title>
  11. <style type="text/css">
  12. <!--
  13.  
  14. -->
  15. </style>
  16. <script type="text/javascript">
  17. <!--
  18.  
  19. var randomNumbers = function( countUpTo ) {
  20. var sCount = 1;
  21. var count = [ ];
  22. while( countUpTo >= sCount ) {
  23. count.push( countUpTo-- );
  24. count.sort( function() { return .5- Math.random(); } );
  25. } for ( var x = 0; x < count.length; x++ ) {
  26. document.writeln( String( count[ x ] ).fontcolor("green") + "<br>" );
  27. }
  28. }
  29.  
  30. // -->
  31. </script>
  32. </head>
  33. <body>
  34. <div id="output">
  35. <script type="text/javascript">
  36. <!--
  37. randomNumbers( 10 );
  38. // -->
  39. </script>
  40. </div>
  41. </body>
  42. </html>

hope it help...
essential
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC