Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I'm trying to create a random 6 digit code. If I generate a random number between 0 and 999999 and then pad it with zeros to make it 6 digits is it random or is it biased in some way?

E.g It generates 750 so it is then padded to 000750 It generates 28149 so it is padded to 028149

Does this achieve the same as generating a random integer between 0 and 9 six times and then putting the numbers together in a string?

Thanks.

share|improve this question
1  
The title of this question amused me. I imagined it to go something like, "Here is a number: 9. Is this number random?" –  Rahul May 20 '12 at 22:45

2 Answers 2

up vote 2 down vote accepted

It makes no difference, provided that in both cases you’re generating the numbers with uniform distribution. Consider, for instance, the probability of generating a number that starts with at least three zeroes. The first method gives you $1000$ such numbers out of $1,000,000$ possibilities, so the probability of getting one is $\frac{1000}{1,000,000}=\frac1{1000}$. In the second method you must generate three zeroes in succession to start, after which you don’t care: the probability of that is $\left(\frac1{10}\right)^3=\frac1{1000}$.

More generally, each of the $10^6$ possible six-digit numbers has one chance in a million of being generated under either scheme.

share|improve this answer

The two procedures should both yield "random" strings of $6$ digits, with all strings equally likely. There may be subtle differences, since we are using pseudo-random number generators.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.