Generate random number with restrictions

I am new to programming in general, so I try to be as specific as possible in this matter. There is this book that I am doing some exercises. I managed to do more than half of what they say, but this is just one piece of material that I struggled to find out.

I will write a question and after that my code,

β€œWrite down an application that creates and prints a random phone number of the form XXX-XXX-XXXX. Include a dash on the output. Do not let the first three digits contain 8 or 9 (but no more restrictive), and make sure that the second set of three digits is no more 742. Hint. Consider the easiest way to build a phone number. Each number does not have to be determined separately. "

OK, the highlighted sentence is what I'm looking at. Here is 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 can i do this? I am in Chapter 3, so we have not discussed if there exist etc. But aliases, String class, packages, import declaration, random class, math class, formatting output (decimal- and numberFormat), Printf, Enumeration classes and Wrapper + autoboxing, so consider a question based only on these assumptions, please.

There are no errors in the code.

Thanks!

+4
source share
7 answers

Seeing that this seems like homework, it seems to me that an explanation of what is happening should be given.

You have three sets of numbers to create.

The first number has most of the requirements. It must be greater than 100, but not contain 8 or 9.

You guarantee that it will always be more than 100 using:

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

This means generating a random number between 0 and 6. Add 1 to this number to make sure it can never be 0. Therefore, if he chooses 0, +1 is added, making him 1. If he chooses 6, +1 is added, making it 7 etc. This satisfies rule No. 1 and rule No. 2.

You guarantee that the first number never has 8 or 9.

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

Create a random number from 0 to 7. * 10 ensures that it will be in the tenth position, while the latter puts the number in the last position.

Instead of trying to fix another 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 
+8
source

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

For three digits, any number from 0 to 741 should work.

For a final set of four digits, any number from 0 to 9999 should work.

This is how you format the output. You can do this using the NumberFormat object, but I decided to do it using the String.format() method. In it you indicate how you want each number to be formatted. So, I used the format string "%d%d%d-%03d-%04d" . %d inserts an integer in format 10 into a string. %03d ensures that it is three characters wide and that any extra space remains with the addition of 0 . In other words, 4 formatted as "004" and 27 formatted as "027" . %04d works similarly except for four characters.

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); `
+4
source

Hmm, just a very simple idea to change this to

 num1 = rand.nextInt(8)*100 + rand.nextInt(8)*10 + rand.nextInt(8); num2 = rand.nextInt(743); 

To simplify the output, you can use DecimalFormat

 DecimalFormat df3 = new DecimalFormat ( "000" ); // 3 zeros DecimalFormat df4 = new DecimalFormat ( "0000" ); // 4 zeros System.out.println(df3.format(num1)+"-"+df3.format(num2)+"-"+df4.format(num3)); 
+1
source

I had the same question as the first task for my Java class, with the caveat that we could only use the methods that we had learned in the class up to this point. Therefore, we could not use the .format method. Here is what I came up with:

 import java.util.Random; /** * * @author */ public class Randnum { /** * @param args the command line arguments */ public static void main(String[] args) { Random num = new Random(); int num0, num1, num2, num3, num4, num5, num6, num7; num0 = num.nextInt(7) + 1; num1 = num.nextInt(8); num2 = num.nextInt(8); num3 = num.nextInt(643) + 101; num4 = num.nextInt(10); num5 = num.nextInt(10); num6 = num.nextInt(10); num7 = num.nextInt(10); String randnum= "A random phone number: "; System.out.print (randnum); System.out.print (num0); System.out.print (num1); System.out.print (num2); System.out.print ("-" + num3 + "-"); System.out.print (num4); System.out.print (num5); System.out.print (num6); System.out.println (num7); } } 
0
source

You can also check that the prefix is ​​not 555

0
source
 var num1 = Math.floor(Math.random()*7) + 1; var num2 = Math.floor(Math.random()*8); var num3 = Math.floor(Math.random()*8); var part2 = Math.floor(Math.random()*742); if(part2 < 100){ part2 = "0" + part2; }else if (part2 < 10) { part2= "00" + part2; }else{ part2 = part2; } var part3 = Math.floor(Math.random()*10000); if(part3 < 1000){ part3 = "0" + part3; }else if (part3 < 100) { part3 = "00"+ part3; }else if(part3 < 10){ part3 = "000" + part3; }else{ part3 = part3; } document.getElementById("demo").innerHTML= " " + num1 + num2 + num3 + "-" + part2 + "-" + part3; 
0
source
 public String GetRandomPhone(){ return String.format("(%03d) %03d-%04d", (int) Math.floor(999*Math.random()), (int) Math.floor(999*Math.random()), (int) Math.floor(9999*Math.random())); } 
-2
source

Source: https://habr.com/ru/post/1333943/


All Articles