Pages

Monday, 1 October 2012

Random number Application in Java Script

Random number are number which make
a random sequence of numbers.In many appliaction we need random number
sequence,for example creating roll number between 1 to 10, in java script you can get random number using Math.random()
which return a floating point number less than zero.
You can get number within a range by multiplying. If you want
integer number then use can use parseInt function..

Create Random Number Between 1 to 10
         function ranNumber()
        {
                    alert(parseInt(Math.random()*10));
        }
HTML..
   <body>
       <input type="button" value="Get Random Number" onclick="ranNumber()" />
  </body>

Change Text Color Randomly ..
         function ChangeTextColor()
        {
            var ob=document.getElementById('s3');
            ob.style.color='#'+parseInt(Math.random()*10)+''+parseInt(Math.random()*10)+''+parseInt(Math.random()*10)+''+parseInt(Math.random()*10)+''+parseInt(Math.random()*10)+''+parseInt(Math.random()*10);
            setTimeout("fun2()",1000);
        }

HTML...
<body onload= "ChangeTextColor()">
            <span id="s1"></span>
</body>

Change Text Color..
         function ChangeTextSize()
        {
            var ob=document.getElementById('s1');
            ob.style.fontSize=parseInt(Math.random()*100)+'px';
            setTimeout("ani()",1000);
        }
HTML...
<body onload= "ChangeTextSize()">
            <span id="s1"></span>
</body>
Move A Divison Randomly
         function movedivison()
        {
            var ob=document.getElementById('d1');
            ob.style.top=parseInt(Math.random()*800);
            ob.style.left=parseInt(Math.random()*800);
            setTimeout("movedivison()",1000);
         }
HTML...
<body onload= "ChangeTextSize()">
           <div id=d1 style="height:100px;width:100px;position:absolute;background-color:Aqua">      
             </div>
</body>

No comments:

Post a Comment