Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Math.Random()

1. ## Math.Random()

I get the fact that it's an easy concept. The lab my teacher wanted us to construct is a random phone number generator.
The area code has to be 218, 312, or 731, it can't be anything else. He told us that we can use Math.random() for this method, but I cannot think of a way how the computer can generate those numbers unless there's an if statement. Any help? I'll post my code if needed. I just don't know the concept I am supposed to use.

3. ## Re: Math.Random()

can you explain the three options: 218, 312, 731. Do you want the program to randomly pick one of these 3 and then add 8 more random digits to create a 11 digit number?

4. ## Re: Math.Random()

to use the Math.Random() class firstly:

import the Random class:
`import java.util.Random;`

then in your main class section to create a Random object:
`Random rand = new Random();`

then to create for example 11 random digits ... :

```digit1 = rand.nextInt(10);
digit2 = rand.nextInt(10);
digit3 = rand.nextInt(10);
digit4 = rand.nextInt(10);
digit5 = rand.nextInt(10);
digit6 = rand.nextInt(10);
digit7 = rand.nextInt(10);
digit8 = rand.nextInt(10);
digit9 = rand.nextInt(10);
digit10 = rand.nextInt(10);
digit11 = rand.nextInt(10);```

rand.nextInt(10) generates a random number between 0 - 9 inclusive .

This should help you.

Get back if you've any more questions.

5. ## Re: Math.Random()

First off, he wanted the area code to be only these, but random each time: 218, 312, 731.
The next three numbers needed like a range from 231 to 750. So I set up a variable
Then the last four numbers were to be from range 0-9999 (except for 1212).
So I set up an if statement.
The gist of the code looks like this, I left my file on my school computer.
```int areacode;
areacode = //problem here, IDK what to put
int middlethree;
middlethree =(int)(231 + 519 * Math.random());
int lastfour;
lastfour = (int)(9999 * Math.random());
if (lastfournumbers != 1212) {
System.out.printf("%3d, %3d, %3d", areacode + middlethree + lastfournumbers);
}```

6. ## Re: Math.Random()

Originally Posted by xionyus
I get the fact that it's an easy concept. The lab my teacher wanted us to construct is a random phone number generator.
The area code has to be 218, 312, or 731, it can't be anything else. He told us that we can use Math.random() for this method, but I cannot think of a way how the computer can generate those numbers unless there's an if statement. Any help? I'll post my code if needed. I just don't know the concept I am supposed to use.
First, create your Random object
`Random gen = new Random();`
You have three options, and you need to randomly choose one.
`int areaCodeType = gen.nextInt(3);`
Then, based on that randomly generated number, choose your area code
```int areaCode = -1;
switch(areaCodeType)
{
case 0:
areaCode = 218;
break;
case 1:
areaCode = 312;
break;
case 2:
areaCode = 731;
break;
}```