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

Recommended Answers

All 2 Replies

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.

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);
    }
}

You could also try this simple variation of using arrays instead of integer values.

All codes is as follows:

<!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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.