Showing posts with label Random Number. Show all posts
Showing posts with label Random Number. Show all posts

Friday, 20 December 2013

Generating a random number in JavaScript

 

Generating a Random Number in JavaScript in range 0 to 9

function randomString() {

            var chars = "0123456789";
            var string_length = 1;
            var randomstring = '';
            for (var i = 0; i < string_length; i++) {
                var rnum = Math.floor(Math.random() * chars.length);
                randomstring += chars.substring(rnum, rnum + 1);
            }
            alert(randomstring); //alert will show the random number
        } 

//setInterval call randomString() function in every 2 second

SB = setInterval(function() { randomString(); }, 2000);



Generating a Random Number in JavaScript by passing the minimum and maximum value

//Here you can generate random number my providing min and max value

function getRandomInt(min, max) {           
            var randomString = Math.floor(Math.random() * (max - min + 1)) + min;           
            alert(randomString);         
        }