javascript - Generating unique 6 digit code js -
i trying generate 6 digit code using js.
it must contains 3 digits , 3 chars.
here code
var numbers = "0123456789"; var chars = "acdefhiklmnoqrstuvwxyz"; var string_length = 3; var randomstring = ''; var randomstring2 = ''; (var x = 0; x < string_length; x++) { var letterornumber = math.floor(math.random() * 2); var rnum = math.floor(math.random() * chars.length); randomstring += chars.substring(rnum, rnum + 1); } (var y = 0; y < string_length; y++) { var letterornumber2 = math.floor(math.random() * 2); var rnum2 = math.floor(math.random() * numbers.length); randomstring2 += numbers.substring(rnum2, rnum2 + 1); } var code = randomstring + randomstring2;
the code result 3chars + 3 numbers .. want rearrange value random value contains same 3 chars , 3 numbers
you shuffle current codes function (from this answer)
//+ jonas raoni soares silva //@ http://jsfromhell.com/array/shuffle [v1.0] function shuffle(o){ //v1.0 for(var j, x, = o.length; i; j = math.floor(math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); return o; };
you use this:
alert(shuffle("stackoverflow".split('')).join(''));
here updated demo code.
Comments
Post a Comment