Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 10 of 10

11142012, 06:05 AM #1
 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 (99999999) // Max (99999999) // Digit limit // Min (14) // Max (14) 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; }

11142012, 07:54 AM #2
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 19,116
 Thanks
 211
 Thanked 2,654 Times in 2,630 Posts
As you say, the code can be simplified, but otherwise seems to work OK. The number generated is random (actually pseudorandom, but that is the best you can do!)
Code:<script type = "text/javascript"> // Generates a random integer // Number range // Min (99999999) // Max (99999999) // Digit limit // Min (14) // Max (14) function generateRandomInteger(minNumber, maxNumber, minDigits, maxDigits) { var len = 0; // initialise len while ((len > maxDigits)  (len < 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(); len = n.length; // If number is negative subtract 1 from length because of "" sign if (num < 0) {len} // End: find number of digits } return num; } alert (generateRandomInteger(100, 5000, 3, 4) ); </script>
"Never attribute to malice that which can be adequately explained by stupidity."  Napoleon BonaparteLast edited by Philip M; 11142012 at 08:31 AM.
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.

11142012, 08:25 AM #3
 Join Date
 Jun 2008
 Posts
 158
 Thanks
 2
 Thanked 16 Times in 16 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() * ( maxmin+1 ) ) ); rands=rands.toString(); var returner=''; for(var i =0; i<digits; i++){ returner+=rands[i]; if(isNaN(rands[i])){digits++;} } return parseInt(returner); }
Last edited by Labrar; 11142012 at 09:06 AM.

11142012, 08:32 AM #4
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 19,116
 Thanks
 211
 Thanked 2,654 Times in 2,630 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.

11142012, 09:04 AM #5
 Join Date
 Jun 2008
 Posts
 158
 Thanks
 2
 Thanked 16 Times in 16 Posts
Sorry. How? is it [JS], [JavaScript] ???
Edit: got it
Is there any button to do this?Last edited by Labrar; 11142012 at 09:07 AM.

11142012, 09:12 AM #6
 Join Date
 Oct 2012
 Posts
 81
 Thanks
 0
 Thanked 4 Times in 4 Posts
Edit:
wrong solution
Last edited by minder; 11142012 at 09:34 AM.

11142012, 09:21 AM #7
 Join Date
 Dec 2010
 Posts
 2,398
 Thanks
 12
 Thanked 570 Times in 563 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)

11142012, 09:34 AM #8
 Join Date
 Oct 2012
 Posts
 81
 Thanks
 0
 Thanked 4 Times in 4 Posts
yep you're right  deleted my post

11142012, 09:46 AM #9
 Join Date
 Sep 2005
 Location
 Sydney, Australia
 Posts
 9,224
 Thanks
 4
 Thanked 926 Times in 913 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,minDigits1); if (minNum<0) minDigNum *= 1; else minDigNum = Math.max(maxNum, minDigNum); maxDigNum = Math.pow(10,maxDigits1); if (maxNum<0) { maxDigNum *= 1; if (maxDigNum<minDigNum) maxDigNum = minDigNum = maxNum; } else maxDigNum = Math.max(minDigNum, maxDigNum); range =minDigNumminNum+maxNummaxDigNum+2; if (minDigNum===maxDigNum) range; num = Math.floor(Math.random()*range)+minNum; if (num > minDigNum) num += maxDigNum; return num; }
Last edited by felgall; 11142012 at 09:49 AM.
Stephen
Learn Modern JavaScript  http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";
which makes it easier to find errors in your code.

11142012, 10:04 AM #10
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 19,116
 Thanks
 211
 Thanked 2,654 Times in 2,630 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.