Thursday, August 28, 2008

Generating artificial phone numbers


Having worked for a company that developed phone fraud detection software in a previous life, I was familiar with the North American Numbering Plan described in Wikipedia. Several times I’ve had a need to create artificial phone numbers.


Below is an algorithm that will handle most if not all of these numbers.



private string GetRandomPhone()


{


    // Generate a NANP compliant phone number (look in wikipedia under North American Numbering Plan)


    var rnd = new Random();


 


    // A number consists of 3 parts: NPA Nxx Station


    // NPA (Numbering Plan Area code) = [2-9][0-8][0-9]


    // Exclude common toll and toll-free numbers


    int npa = 0;


    while (npa == 0 || npa == 800 || npa == 877 || npa == 888 || npa == 900)


    {


        npa = rnd.Next(2, 9) * 100 + rnd.Next(0, 8) * 10 + rnd.Next(9);


    }


 


    // NXX (Central Office or Exchange code) = [2-9][0-9][0-9]


    // Exception the second digits may not be 11


    int nxx = 0;


    while (nxx == 0 || nxx % 100 == 11)


    {


        nxx = rnd.Next(2, 9)*100 + rnd.Next(99);


    }


 


    // Station code = [0-9][0-9][0-9][0-9]


    // When 555 is the NXX the number 100-199 are reserved or fictional numbers)


    int station = 0;


    while (station == 0 || (nxx == 555 && station > 99 && station < 200))


    {


        station = rnd.Next(9999);


    }


 


    const string format = "({0:000}) {1:000}-{2:0000}";


    return string.Format(format, npa, nxx, station);


}


 


Now playing: Hans Zimmer & James Newton Howard - Macrotus

No comments:

Post a Comment