Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate random number with restrictions

Tags:

java

random

math

I'm new to programming in general so I'm trying to be as specific as possible in this question. There's this book that I'm doing some exercises on. I managed to do more than half of what they say, but it's just one input that I have been struggling to find out.

I'll write the question and thereafter my code,

"Write an application that creates and prints a random phone number of the form XXX-XXX-XXXX. Include the dashes in the output. Do not let the first three digits contain an 8 or 9 (but don't be more restrictive than that), and make sure that the second set of three digits is not greater than 742. Hint: Think through the easiest way to construct the phone number. Each digit does not have to be determined separately."

OK, the highlighted sentence is what I'm looking at. Here's my code:

import java.util.Random;
public class PP33 {
  public static void main (String[] args) {
    Random rand = new Random();

    int num1, num2, num3;

    num1 = rand.nextInt (900) + 100;
    num2 = rand.nextInt (643) + 100;
    num3 = rand.nextInt (9000) + 1000;

    System.out.println(num1+"-"+num2+"-"+num3);
  }
}

How am I suppose to do this? I'm on chapter 3 so we have not yet discussed if statements etcetera, but Aliases, String class, Packages, Import declaration, Random Class, Math Class, Formatting output (decimal- & numberFormat), Printf, Enumeration & Wrapper classes + autoboxing. So consider answer the question based only on these assumptions, please.

The code doesn't have any errors.

Thank you!

like image 909
Racket Avatar asked Jan 01 '11 16:01

Racket


2 Answers

Seeing as this appears to be homework I feel like an explanation of what is happening should be given.

You have three sets of numbers you need to generate.

The first number has the most requirements. It has to be greater than 100 but not contain an 8 or 9.

You ensure it will always be greater than 100 by using:

(rand.nextInt(7)+1) * 100. 

This says, generate a random number between 0 and 6. Add 1 to that number to ensure that it can never be 0. So if it picks 0, +1 is added making it 1. If it picks 6, +1 is added making it 7, etc. This satisfies rule #1 and rule #2.

You ensure the first number never has an 8 or 9.

(rand.nextInt(8) * 10) + rand.nextInt(8)

Genreate a random number from 0-7. The *10 makes sure it will be in the tenth position while the last one places the number in the last position.

Instead of trying to fix the other answer as it also uses DecimalFormat incorrectly.

package stackoverflow_4574713;

import java.text.DecimalFormat;
import java.util.Random;

public class Main {
         public static void main(String[] args) {

        Random rand = new Random();
        int num1 = (rand.nextInt(7) + 1) * 100 + (rand.nextInt(8) * 10) + rand.nextInt(8);
        int num2 = rand.nextInt(743);
        int num3 = rand.nextInt(10000);

        DecimalFormat df3 = new DecimalFormat("000"); // 3 zeros
        DecimalFormat df4 = new DecimalFormat("0000"); // 4 zeros

        String phoneNumber = df3.format(num1) + "-" + df3.format(num2) + "-" + df4.format(num3);

        System.out.println(phoneNumber);
    }
}

Output:

662-492-1168
like image 90
Andrew T Finnell Avatar answered Oct 11 '22 12:10

Andrew T Finnell


For the first three digits, you need to generate each digit separately. See variables i1, i2, and i3 below.

For the three digits, any number between 0 and 741 should work.

For the final set of four digits, any number between 0 and 9999 should work.

The trick here is how you format the output. You could do it with a NumberFormat object, but I chose to do it with the String.format() method. In it, you specify how you want each number to be formatted. So, I used the format string "%d%d%d-%03d-%04d". The %d inserts a base-10 formatted integer into the string. The %03d makes sure that it is three characters wide and that any additional space is left-padded with a 0. In other words, 4 is formatted as "004" and 27 is formatted as "027". The %04d works similarly, except it is four characters wide.

Here's how you put it all together.

Random r = new Random();

int i1 = r.nextInt(8); // returns random number between 0 and 7
int i2 = r.nextInt(8);
int i3 = r.nextInt(8);
int i4 = r.nextInt(742); // returns random number between 0 and 741
int i5 = r.nextInt(10000); // returns random number between 0 and 9999

String phoneNumber = String.format("%d%d%d-%03d-%04d", i1, i2, i3, i4, i5);
System.out.println(phoneNumber);`
like image 20
pwc Avatar answered Oct 11 '22 12:10

pwc