Last active
April 18, 2017 20:27
-
-
Save jayantbh/edfec72fb3d6745daaf0d23771d642da to your computer and use it in GitHub Desktop.
Generate random N-digit phone number with or without specific prefixes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let prefix = ['98', '78', '97', '96', '89', '99', '70', '79', '90', '94', '76', '84', '86', '81']; | |
function randomInt(a = 0, b = 0) { return (a + window.crypto.getRandomValues(new Uint8Array(1))[0] % Math.abs(b - a)).toString(); } | |
Uint32Array.prototype.unique = function () { return this.filter((v, i) => ( this.indexOf(v) == i )); } | |
function numbers(n = 16384, usePrefix, len) { | |
let seed = randomInt(0,99999999); | |
let numbers = window.crypto.getRandomValues(new Uint32Array(n)).unique(); | |
numbers.forEach((n) => { | |
let pfx = usePrefix ? prefix[n % prefix.length] : ''; | |
let length = len - pfx.length; | |
console.log(pfx + (n*seed).toString().slice(0,length)) | |
}); | |
console.log(numbers.length); | |
} | |
numbers(150, true, 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment