-2

My Friend ask me this question and i am posting it here, if you have better approach please share

 String getRandomMobileNumber() {
        double random = Math.random();
        Double randomten = random*1000000000.0;
        return "0"+Math.round(randomten);
    }
Munish Chouhan
  • 318
  • 4
  • 10
  • 1
    You should prefer the `Random` class over `Math.random()`. – Kayaman Sep 12 '19 at 08:40
  • I'm pretty confident that will _very_ rarely generate a valid phone number... – BeUndead Sep 12 '19 at 08:42
  • You're more likely to get a response if you provide information on the phone numbering scheme for your country, and maybe also a bit of context behind the requirement to put our minds at ease that you won't be using your code to make spam calls to random people. – jsheeran Sep 12 '19 at 08:46
  • 2
    You can [answer](https://stackoverflow.com/help/self-answer) your own questions, but you should [ask them first](https://stackoverflow.com/help/how-to-ask), and say what you like (or do not like) about your current approach. As it stands, you have just posted some code. There's lots of code out there: what problem is yours solving? Why is it good, or better than other approaches? Without a question, it is hard to know. – tucuxi Sep 12 '19 at 09:00
  • One of my friend ask me this question, he wants random mobile numbers start with and have 9 numbers and then i thought if anyone else have this question they can use this question. – Munish Chouhan Sep 12 '19 at 12:12
  • My intention was only to help, but giving this question negative points is not correct – Munish Chouhan Sep 12 '19 at 12:13
  • I have edited the question, which makes more sense – Munish Chouhan Sep 12 '19 at 12:20

1 Answers1

3

You can use this, see javadoc:

String phoneNumber = "0" + ThreadLocalRandom.current().nextInt(10000000, 99999999);

tostao
  • 2,803
  • 4
  • 38
  • 61