generate 4 digit random number using substring
89,881
Solution 1
That's because a
is a number, not a string. What you probably want to do is something like this:
var val = Math.floor(1000 + Math.random() * 9000);
console.log(val);
-
Math.random()
will generate a floating point number in the range [0, 1) (this is not a typo, it is standard mathematical notation to show that 1 is excluded from the range). - Multiplying by 9000 results in a range of [0, 9000).
- Adding 1000 results in a range of [1000, 10000).
- Flooring chops off the decimal value to give you an integer. Note that it does not round.
General Case
If you want to generate an integer in the range [x, y), you can use the following code:
Math.floor(x + (y - x) * Math.random());
Solution 2
This will generate 4-digit random number (0000-9999) using substring:
var seq = (Math.floor(Math.random() * 10000) + 10000).toString().substring(1);
console.log(seq);
Solution 3
I adapted Balajis to make it immutable and functional.
Because this doesn't use math you can use alphanumeric, emojis, very long pins etc
const getRandomPin = (chars, len)=>[...Array(len)].map(
(i)=>chars[Math.floor(Math.random()*chars.length)]
).join('');
//use it like this
getRandomPin('0123456789',4);
Solution 4
$( document ).ready(function() {
var a = Math.floor(100000 + Math.random() * 900000);
a = String(a);
a = a.substring(0,4);
alert( "valor:" +a );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
Solution 5
$(document).ready(function() {
var a = Math.floor((Math.random() * 9999) + 999);
a = String(a);
a = a.substring(0, 4);
});
Author by
aviate wong
Updated on July 09, 2022Comments
-
aviate wong almost 2 years
I am trying to execute below code:
var a = Math.floor(100000 + Math.random() * 900000); a = a.substring(-2);
I am getting error like
undefined is not a function
at line 2, but when I try to doalert(a)
, it has something. What is wrong here? -
Sumner Evans about 9 yearsThis generates a number with 6 digits rather than 4.
-
Felix Kling about 9 yearsIt also doesn't create a number but a string.
-
Felix Kling about 9 yearsYour demo clearly shows that your solution produces a string of length 6.
-
Shelly about 9 yearsyeah thats what i am trying to do
-
Felix Kling about 9 yearsWell, the OP wants to create a 4 digit number.
-
Isaac Stevens about 9 yearsgot to remember that when you do a math logic you problably will receive a data type as either int or double, therefore you got to convert the result to string to be handle by the substring function, hope this helps
-
Felix Kling about 9 yearsHow is this different from erkaner's solution? The
$(document).ready
call is unnecessary here. -
Isaac Stevens about 9 yearsThats in case you use it in jquery code, but you are right otherwise is unnecessary
-
5ervant - techintel.github.io over 6 yearsI can't regenerate a code that starts with a zero, such as
0123
. -
Sumner Evans over 6 years@5ervant, that would be a 3 digit number. (Mathematically, the leading 0 does not matter.)
-
Balaji Ts almost 5 yearsit will generate random value from char variable with length 5 & in loop it will check the length and generate random 5 digit value from char variable and convert it to uppercase