Skip to content

Instantly share code, notes, and snippets.

@joeyv
Last active September 21, 2023 13:41
Show Gist options
  • Save joeyv/7087747 to your computer and use it in GitHub Desktop.
Save joeyv/7087747 to your computer and use it in GitHub Desktop.
Generates a random phone number
import java.util.*;
public class Phone
{
public static void main(String[] args)
{
int num1, num2, num3; //3 numbers in area code
int set2, set3; //sequence 2 and 3 of the phone number
Random generator = new Random();
//Area code number; Will not print 8 or 9
num1 = generator.nextInt(7) + 1; //add 1 so there is no 0 to begin
num2 = generator.nextInt(8); //randomize to 8 becuase 0 counts as a number in the generator
num3 = generator.nextInt(8);
// Sequence two of phone number
// the plus 100 is so there will always be a 3 digit number
// randomize to 643 because 0 starts the first placement so if i randomized up to 642 it would only go up yo 641 plus 100
// and i used 643 so when it adds 100 it will not succeed 742
set2 = generator.nextInt(643) + 100;
//Sequence 3 of numebr
// add 1000 so there will always be 4 numbers
//8999 so it wont succed 9999 when the 1000 is added
set3 = generator.nextInt(8999) + 1000;
System.out.println ( "(" + num1 + "" + num2 + "" + num3 + ")" + "-" + set2 + "-" + set3 );
}
}
@ChristolSara
Copy link

thnx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment