943,723 Members | Top Members by Rank

Ad:
Jun 19th, 2009
0

how do i fill random number?

Expand Post »
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
Similar Threads
k2k
Reputation Points: 15
Solved Threads: 1
Posting Whiz
k2k is offline Offline
351 posts
since Nov 2007
Jun 19th, 2009
0

Re: how do i fill random number?

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)
  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. }
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Jun 19th, 2009
0

Re: how do i fill random number?

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
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Form Validation
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: div image long link problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC