# Creating Phone Numbers with Random Class

Page 1 of 1

## 7 Replies - 479 Views - Last Post: 04 October 2012 - 09:59 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294274&amp;s=53446696e5f18efeaa9062b95a11dc04&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mkorb

Reputation: 0
• Posts: 7
• Joined: 04-October 12

# Creating Phone Numbers with Random Class

Posted 04 October 2012 - 12:13 PM

Using the Random class, write a program that generates a random phone number of the form XXX-XXX-XXXX where the first group of three digits may not start with a 0 or a 1, the middle three digits must form an even number between 000 and 998, and the last group of four digits must be between 0000 and 5999. Your program may place no other restrictions on any of the groups of numbers. Exactly 10 digits and the two dashes must be printed.

For some reason I can't get the middle numbers to be all even and the first three numbers I can't get to be different. Here is what i have so far...
```import java.util.Random;

public class PhoneNumber
{

public static void main(String args[])

{
Random generator = new Random();
int num1,num2,num3;

num1 = generator.nextInt(9) + 2 ; // returns random number between 0 and 7
num1 = generator.nextInt(7) - 3;
num1 = generator.nextInt(6) + 4;
num2 = generator.nextInt(999) - 2 ; // returns random number between 0 and 741
num3 = generator.nextInt(6000); // returns random number between 0 and 9999

String phoneNumber = String.format("%d%d%d-%03d-%04d", num1, num1, num1, num2, num3);
System.out.println(phoneNumber);

}
```

This post has been edited by jon.kiparsky: 04 October 2012 - 12:16 PM
Reason for edit:: added code tags

Is This A Good Question/Topic? 0

## Replies To: Creating Phone Numbers with Random Class

### #2 jon.kiparsky

• Pancakes!

Reputation: 5605
• Posts: 9,048
• Joined: 19-March 11

## Re: Creating Phone Numbers with Random Class

Posted 04 October 2012 - 12:21 PM

```num1 = generator.nextInt(9) + 2 ; // returns random number between 0 and 7
```

No it doesn't. It generates a random number n, such that 0 <= n <9 and then it adds 2 to it.

```	num2 = generator.nextInt(999) - 2 ; // returns random number between 0 and 741
```

Where does 741 come from?

Quote

For some reason I can't get the middle numbers to be all even

One way to think about this is to generate random numbers and test them until you get an even one.
Another way is to create a random number that you know will be even.

Here's a hint: how many three-digit even numbers are there? Can they be mapped onto any other set of integers?

### #3 no2pencil

• Original Digital Gansta

Reputation: 4503
• Posts: 24,971
• Joined: 10-May 07

## Re: Creating Phone Numbers with Random Class

Posted 04 October 2012 - 12:23 PM

Please be mindful of your topic when posting. Being that this is a support forum, in most cases members are posting questions.

I have updated the title to be more descriptive of the question.

### #4 mkorb

Reputation: 0
• Posts: 7
• Joined: 04-October 12

## Re: Creating Phone Numbers with Random Class

Posted 04 October 2012 - 12:25 PM

Sorry about that when I was changing things around trying to fix it, I ignored changing the comments.
```import java.util.Random;

public class PhoneNumber
{

public static void main(String args[])

{
Random generator = new Random();
int num1,num2,num3;

num1 = generator.nextInt(9) + 2 ; // first group may not start with 0 or a 1
num1 = generator.nextInt(7) + 2;
num1 = generator.nextInt(6) + 2;
num2 = generator.nextInt(999) - 2 ; //must form an even number between 000-998
num3 = generator.nextInt(6000); //must be between 0000-5999

String phoneNumber = String.format("%d%d%d-%03d-%04d", num1, num1, num1, num2, num3);
System.out.println(phoneNumber);
```

This post has been edited by jon.kiparsky: 04 October 2012 - 12:29 PM
Reason for edit:: added code tags (again)

### #5 jon.kiparsky

• Pancakes!

Reputation: 5605
• Posts: 9,048
• Joined: 19-March 11

## Re: Creating Phone Numbers with Random Class

Posted 04 October 2012 - 12:29 PM

mkorb, on 04 October 2012 - 02:25 PM, said:

Sorry about that when I was changing things around trying to fix it, I ignored changing the comments.

As a side note, this is a good reason to beware of comments of this sort. They tend to get out of step with the code. I can tell what you're doing, tell me why you're doing it.

This post has been edited by jon.kiparsky: 04 October 2012 - 07:20 PM

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8066
• Posts: 31,310
• Joined: 06-March 08

## Re: Creating Phone Numbers with Random Class

Posted 04 October 2012 - 07:20 PM

what is the use of regenerating 3 times the value stored in num1 ???

num1 = generator.nextInt(9) + 2 ; // first group may not start with 0 or a 1
num1 = generator.nextInt(7) + 2;
num1 = generator.nextInt(6) + 2;

only the last one will be kept

### #7 mkorb

Reputation: 0
• Posts: 7
• Joined: 04-October 12

## Re: Creating Phone Numbers with Random Class

Posted 04 October 2012 - 09:49 PM

num1 = generator.nextInt(9)+2 ;
num2 = generator.nextInt(999) ; //must form an even number between 000-998
num3 = generator.nextInt(6000); //must be between 0000-5999

String phoneNumber = String.format("%d%d%d-%03d-%04d", num1, num2, num3);
System.out.println(phoneNumber);

whenever i try to run it, it gives me an error message and it's because of the format, when i try to change %d to %03d it gives me a 0 as the first number in the start of the phone number which is not allowed as it is stated in the instructions. I can't figure out how to make the first three numbers not start with 0 or 1 and I can't figure out how to make the middle 3 all even numbers, in other words how to I rule out odd numbers?

### #8 jon.kiparsky

• Pancakes!

Reputation: 5605
• Posts: 9,048
• Joined: 19-March 11

## Re: Creating Phone Numbers with Random Class

Posted 04 October 2012 - 09:59 PM

Quote

I can't figure out how to make the middle 3 all even numbers, in other words how to I rule out odd numbers?

One way would be to generate numbers until you get one which meets the definition of "even". There are several definitions you could use, but the most useful one is "is evenly divisible by 2". That's going to require a while loop and an unknown amount of cpu time - not much, in any case, but certainly more than you need to use, because you can use another approach - again, think about the definition of "even", and think about how many even numbers there are between zero and 1000.