Skip to content

Instantly share code, notes, and snippets.

@ianldgs
Last active August 29, 2015 14:22
Show Gist options
  • Save ianldgs/ce68b337f301bf0167ad to your computer and use it in GitHub Desktop.
Save ianldgs/ce68b337f301bf0167ad to your computer and use it in GitHub Desktop.
function randomiza(n) {
return Math.round(Math.random()*n);
}
function mod(dividendo,divisor) {
return Math.round(dividendo - (Math.floor(dividendo/divisor)*divisor));
}
function gerarCPF(comPontos) {
var n = 9;
var n1 = randomiza(n);
var n2 = randomiza(n);
var n3 = randomiza(n);
var n4 = randomiza(n);
var n5 = randomiza(n);
var n6 = randomiza(n);
var n7 = randomiza(n);
var n8 = randomiza(n);
var n9 = randomiza(n);
var d1 = n9*2+n8*3+n7*4+n6*5+n5*6+n4*7+n3*8+n2*9+n1*10;
d1 = 11 - ( mod(d1,11) );
if (d1>=10) d1 = 0;
var d2 = d1*2+n9*3+n8*4+n7*5+n6*6+n5*7+n4*8+n3*9+n2*10+n1*11;
d2 = 11 - ( mod(d2,11) );
if (d2>=10) d2 = 0;
var cpf;
if (comPontos) cpf = ''+n1+n2+n3+'.'+n4+n5+n6+'.'+n7+n8+n9+'-'+d1+d2;
else cpf = ''+n1+n2+n3+n4+n5+n6+n7+n8+n9+d1+d2;
return cpf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment