-1
$\begingroup$

To preface, this question is coming from a software developer so it's written from that perspective.

If I need to generate a random number with $n$ digits, I could do it in one of two ways.

a. Ask a random number generator for a digit between $0-9$ and concatenate each number retrieved until I have the required n digits

b. Ask a random number generator for a single number within the range of $100\ldots${$n$-digits}$-999\ldots${$n$-digits}, ie: for a $5$ digit number the range would be $10000-99999$

The obvious difference is that with option A I have a larger number space because the first digit could be 0. Barring that difference, is there anything statistically different between the randomness or the distribution of the resulting numbers from these two methods? Is one preferred over the other?

$\endgroup$
1
$\begingroup$

Mathematically, there is no difference. For example, suppose that I have an RNG1 that gives me a number with uniform distribution on $00,01,02,03,\ldots,97,98,99$. Alternatively, I can design an RNG2 that gives me a random digit uniformly distributed on $0,1,\ldots,9$. I call this RNG2 two times and concatenate the digits. The end distribution is the same as what I would obtain with RNG1. In general, RNGs on computers are however pseudo-random and at the end there might be a difference between the two methods. This website may however not be the right place to ask such a question.

$\endgroup$
  • $\begingroup$ Thanks! This came up at work as a purely academic question and none of us were entirely sure. $\endgroup$ – Jason McClellan Dec 5 '13 at 18:13
  • $\begingroup$ @JasonMcClellan You are welcome. $\endgroup$ – Lord Soth Dec 5 '13 at 19:04
-1
$\begingroup$
/* genNDig.cc 04-dic-2013
http://math.stackexchange.com/questions/593258/what-is-the-statistical-difference-if-any-between-these-two-methods-of-generat
*/
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
typedef long double ldouble;
typedef unsigned long long ullong;

class NDig
{
       private:
               ullong n,dn,nMin;
               static const ldouble randmax1;
       public:
              explicit NDig(ullong nVal);
              operator ullong() const; 
};
const ldouble NDig::randmax1=ldouble(RAND_MAX) + 1.0L;

inline NDig::NDig(ullong nVal): n(nVal)
{
 nMin=static_cast<ullong>(floor(pow(10.0L,ldouble(n - 1ULL)) + 0.5L));
 dn=static_cast<ullong>(floor(pow(10.0L,ldouble(n)) + 0.5L)) - nMin;
}

inline  NDig::operator ullong() const
{
 return nMin + ullong((rand()/randmax1)*dn);
}

int main()
{
 srand(size_t(time((time_t *)0)));
 NDig r(5ULL);

 for ( unsigned char i = 0 ; i<10 ; ++i ) cout<<r<<endl;
 return 0;
}

Result:

92592
69660
27055
94106
42956
54359
81989
75872
31139
78100
$\endgroup$
  • $\begingroup$ I'm sorry, but am I supposed to be gleaning something from the shown results here? I don't see how this answers the question? $\endgroup$ – Jason McClellan Dec 5 '13 at 4:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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