TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

Language: Client Scripting
Expertise: Beginner
Feb 11, 2000

### WEBINAR:On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning

# Generate a Unique Whole Number

Question:
How can I generate a unique number using JavaScript?

You can generate random numbers in JavaScript using the Math.random() method. This function returns a fractional random number between 0 and 1. Unlike other languages, JavaScript will initialize a random seed for you automatically so you don't have to worry about anything else. Of course, in most cases, a fraction isn't what you'll want. A whole number between a low and a high bound would probably be more useful. This simple function called GetRandomNumber does just this:

```

!function(){if(!window.BOOMR||!window.BOOMR.version){var e,t,n,o=document.createElement("iframe");o.src="javascript:false",o.title="",o.role="presentation",(o.frameElement||o).style.cssText="width:0;height:0;border:0;display:none;",(n=document.getElementsByTagName("script")[0]).parentNode.insertBefore(o,n);try{t=o.contentWindow.document}catch(n){e=document.domain,o.src="javascript:var d=document.open();d.domain='"+e+"';void(0);",t=o.contentWindow.document}t.open()._l=function(){var t=this.createElement("script");e&&(this.domain=e),t.id="boomr-if-as",t.src="/imageserver/common/sg/1.0/sg.min.js",BOOMR_lstart=(new Date).getTime(),this.body.appendChild(t)},t.write('<body onload="document._l();">'),t.close()}}();

<!--
function GetRandomNumber(low, high) {
var i;

if (low >= high) {
return null;
} else {
i = Math.random();
return Math.round(low + ((high - low) * i));
}
}
//-->

window.webtrendsAsyncInit=function(){
var dcs=new Webtrends.dcs().init({
dcsid:"dcsetaek200000gcgxfcx07g4_2p3u",
domain:"www.qsstats.com",
timezone:-8,
i18n:true,
onsitedoms:"devx.com",
fpcdom:".devx.com",
plugins:{
//hm:{src:"//s.webtrends.com/js/webtrends.hm.js"}
}
}).track();
};
(function(){
var s=document.createElement("script"); s.async=true; s.src="/imageserver/common/v10/webtrends.min.js";
var s2=document.getElementsByTagName("script")[0]; s2.parentNode.insertBefore(s,s2);
}());

```
DevX Pro

 Submit a Tip Browse "Web Development" Tips Browse All Tips
Comment and Contribute

(Maximum characters: 1200). You have 1200 characters left.

Thanks for your registration, follow us on our social networks to keep up-to-date