Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com JavaScript Function: Generate a random integer within specified range AND digit limit

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 11-14-2012, 07:05 AM PM User | #1 genstonewall New to the CF scene   Join Date: Nov 2012 Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts JavaScript Function: Generate a random integer within specified range AND digit limit I need a function that generates a completely random integer (very important) within a user specified number range (between -9999 to 9999) and a user specified digit limit (between 1 and 4 digits). Example 1: If the user wants a number between -9999 and 9999 that's 4 digits, the following numbers would be eligible choices -9999 to -1000 and 1000 to 9999. Example 2: If the user wants a number between 25 and 200 that's 2 OR 3 digits, the following numbers would be eligible choices 25 to 200. I wrote a function that works but I am not sure if it's the best solution? There's duplicate code and I don't think it's completely random? Thanks. Code: ```// Generates a random integer // Number range // Min (-9999-9999) // Max (-9999-9999) // Digit limit // Min (1-4) // Max (1-4) function generateRandomInteger(minNumber, maxNumber, minDigits, maxDigits) { // Generate a random integer in the number range var num = Math.floor(Math.random() * (maxNumber - minNumber)) + minNumber; // Find number of digits var n = num.toString(); n = n.length; // If number is negative subtract 1 from length because of "-" sign if (num < 0) { n--; } // End: find number of digits while ((n > maxDigits) || (n < minDigits)) { // Generate a random integer in the number range num = Math.floor(Math.random() * (maxNumber - minNumber)) + minNumber; // Find number of digits var n = num.toString(); n = n.length; // If number is negative subtract 1 from length because of "-" sign if (num < 0) { n--; } // End: find number of digits } return num; }```
 11-14-2012, 08:54 AM PM User | #2 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,475 Thanks: 200 Thanked 2,469 Times in 2,447 Posts As you say, the code can be simplified, but otherwise seems to work OK. The number generated is random (actually pseudo-random, but that is the best you can do!) Code: `````` You need to check that the user data is consistent, e.g. not 10,99,3,4 or an infinite loop is set up. "Never attribute to malice that which can be adequately explained by stupidity." - Napoleon Bonaparte __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration. Last edited by Philip M; 11-14-2012 at 09:31 AM..
 11-14-2012, 09:25 AM PM User | #3 Labrar New Coder   Join Date: Jun 2008 Posts: 61 Thanks: 0 Thanked 12 Times in 12 Posts What about Code: ```function randomizer( min, max, digits ) { if(isNaN(digits)){digits=4;} if( min > max ) { return( -1 ); } if( min == max ) { return( min ); } var rands=( min + parseInt( Math.random() * ( max-min+1 ) ) ); rands=rands.toString(); var returner=''; for(var i =0; i
 11-14-2012, 09:32 AM PM User | #4 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,475 Thanks: 200 Thanked 2,469 Times in 2,447 Posts Labrar - please post Javascript code within code tags. Not php tags! That is likely to confuse beginners. __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration.
 11-14-2012, 10:04 AM PM User | #5 Labrar New Coder   Join Date: Jun 2008 Posts: 61 Thanks: 0 Thanked 12 Times in 12 Posts Sorry. How? is it [JS], [JavaScript] ??? Edit: got it Is there any button to do this? Last edited by Labrar; 11-14-2012 at 10:07 AM..
 11-14-2012, 10:12 AM PM User | #6 minder Banned   Join Date: Oct 2012 Posts: 81 Thanks: 0 Thanked 4 Times in 4 Posts Edit: wrong solution Last edited by minder; 11-14-2012 at 10:34 AM..
 11-14-2012, 10:21 AM PM User | #7 devnull69 Senior Coder   Join Date: Dec 2010 Posts: 2,353 Thanks: 11 Thanked 557 Times in 550 Posts Not if you read and understand the OP's post He wants to exclude those random numbers that are not of a certain length Example: Random number with 3 to 4 digits between -10000 and 10000 would exclude those numbers that are only two digits (like -12 or 98)
 11-14-2012, 10:34 AM PM User | #8 minder Banned   Join Date: Oct 2012 Posts: 81 Thanks: 0 Thanked 4 Times in 4 Posts yep you're right - deleted my post
 11-14-2012, 10:46 AM PM User | #9 felgall Master Coder     Join Date: Sep 2005 Location: Sydney, Australia Posts: 6,089 Thanks: 0 Thanked 579 Times in 569 Posts There's no need for a loop. The following code does it with only one call to Math.random as the number returned always maps to within the required range with the same distribution regardless of what numbers are entered. Code: ```function generateRandomInteger(minNumber, maxNumber, minDigits, maxDigits) { var minNum, maxNum, minDigNum, maxDigNum, range, num; minNum = Math.min(minNumber, maxNumber); maxNum = Math.max(minNumber, maxNumber); minDigNum = Math.pow(10,minDigits-1); if (minNum<0) minDigNum *= -1; else minDigNum = Math.max(maxNum, minDigNum); maxDigNum = Math.pow(10,maxDigits-1); if (maxNum<0) { maxDigNum *= -1; if (maxDigNum minDigNum) num += maxDigNum; return num; }``` __________________ Stephen Learn Modern JavaScript - http://javascriptexample.net/ Helping others to solve their computer problem at http://www.felgall.com/ Beginners need to advise whether they want to learn "Latin" JavaScript for Netscape 3 or "Italian" JavaScript for modern browsers. Last edited by felgall; 11-14-2012 at 10:49 AM..
 11-14-2012, 11:04 AM PM User | #10 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,475 Thanks: 200 Thanked 2,469 Times in 2,447 Posts Yet another good example of a different way to skin a cat. __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 01:50 PM.