0

I am trying to create a random phone number with a range. The format being (xxx)-xxx-xxx and the area code not starting with 0,8, or 9 and the next set of three being in a range from 100-742 and then the last set of 4 can be any digit. How would i create the first two parts? Any help would be appreciated. Thanks!

import java.util.*;
import java.text.*;

public class PhoneNumber{
    public static void main(String[] arg){
        Random ranNum = new Random();
        //int areaCode = 0;
        //int secSet = 0;
        //int lastSet = 0;
        DecimalFormat areaCode = new DecimalFormat("(000)");
        DecimalFormat secSet = new DecimalFormat("-000");
        DecimalFormat lastSet = new DecimalFormat("-0000");
        //DecimalFormat phoneNumber = new DecimalFormat("(###)-###-####");
        int i = 0;
        //areaCode = (ranNum.nextInt()); //cant start with 0,8,9
        //secSet = (ranNum.nextInt()); // not greater than 742 and less than 100
        //lastSet = (ranNum.nextInt(999)) + 1; // can be any digits


        i = ranNum.nextInt();
        System.out.print(areaCode.format(i));
        i = ranNum.nextInt();
        System.out.print(secSet.format(i));
        i = ranNum.nextInt();
        System.out.print(lastSet.format(i));

    }
}
BevynQ
  • 8,089
  • 4
  • 25
  • 37
ravalos1991
  • 1
  • 1
  • 4
  • nextInt takes an argument: http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt(int) `areaCode = nextInt(700) + 100;` to start you off. – Radiodef Oct 28 '13 at 22:57

4 Answers4

1

well, basically, you need to generate numbers in two ranges

  • [1; 7]
  • [100; 742]

To have random integer in range [m; n] you could write:
updated (remove Math.random())

int numberInRange = m + new Random().nextInt(n - m + 1);

HTH

kiruwka
  • 9,250
  • 4
  • 30
  • 41
1

1,2,3,4,5,6,7 makes 7 different values

    ranNum.nextInt(7)+1; //So 1 is your lowest number and 7 is the number of different solutions

nexInt will range between 0 and intPassed exclusive,
So ranNum.nextInt(7) will run between 0 and 6, + 1 makes 1 .. 7
This will range between 1 and 7
You can take the same principal for the second range

Mazzy
  • 1,901
  • 2
  • 16
  • 36
0

You can try the next:

int sec = java.util.concurrent.ThreadLocalRandom.current().nextInt(100, 743);

And so on with the other parts of the phone number.

This method returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
0

Just make a random number greater than 99 and less than 800, then the next would be about the same way.

 Random rand = new Random();

// add 1 to make it inclusive
                                   max   min
int firstRandomSet = rand.nextInt((799 - 100) + 1) + 100; 
//none starts with 0,8, or 9


int secondRandomSet = rand.nextInt((742 - 100) + 1) + 100; 
//produces anything from 100-742

to get the numbers 0001 - 9999 you'll have to be creative.

int maxValues= 9999;

       int thirdRandomSet = rand.nextInt(maxValues);

       System.out.printf("%04d\n", thirdRandomSet);
CandyCane
  • 29
  • 5