-4

Is there any inbuilt utility in java that I can use to genrate random numbers ? The format is xxxxxxxxxx and max limit of 10 numbers.

worrynerd
  • 705
  • 1
  • 12
  • 31

3 Answers3

1

You can use Math.random() which returns number between 0 to 1. You can multiply any 1000000000 for getting 10 digit random number.

Math.floor(Math.random()*100000000000)

Check out Math , specially Math.random() and Math.floor() for rounding off

Community
  • 1
  • 1
Panther
  • 3,312
  • 9
  • 27
  • 50
  • This is an unsafe way to get a random integer; some results will be more likely than others due to the [Pigeonhole principle](https://en.wikipedia.org/wiki/Pigeonhole_principle). Use the [`Random.nextInt(int)`](http://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int-) method to generate uniform random integers. – dimo414 Sep 23 '15 at 22:41
0

You're probably looking for the Random class.

For example:

int randomNumber;
Random rand = new Random();
randomNumber = rand.nextInt(1000000000);

This will generate a random number between 0 (inclusive) and 1000000000 (exclusive)

edit: It's definitely worth mentioning that nextInt() does not require an argument, and if none is provided it will choose a random number between 0 (again, inclusive) and 2^32 (also exclusive).

dimo414
  • 47,227
  • 18
  • 148
  • 244
  • @Stephan Bendy Right.... you can also set start for random number generation using.. setSeed(Long long) method.... – Mr. Noddy Sep 22 '15 at 04:28
  • I'm not sure what you mean by that... setting the seed will not make the initial value equal to the seed you entered, but rather make it so that any time you call a series of random numbers with a particular seed the exact same series of numbers will be generated. – Stephen Bendl Sep 22 '15 at 04:32
  • yes you are right @Stephan... – Mr. Noddy Sep 22 '15 at 04:43
  • for generating random number in range you can search for "Generating random integers in a range with Java" on stackoverflow – Mr. Noddy Sep 22 '15 at 04:44
0

The question does not describe what format is xxxxxxxxxx. But you can always use the Random class to generate a random number within a specified range. In case you have a specific format of sequence of characters and numbers then you have to merge based on the format.

import java.util.Random;

public class RandomGenerator {
   public static void main(String[] args) {
      Random random = new Random();
      long randomInt = random.nextInt(999999999);
      System.out.println("Generated : " + randomInt);
   }
}

Alternately you can use the UUID class. A UUID represents a 128-bit value

For example:

String randomNumber = UUID.randomUUID().toString();
dimo414
  • 47,227
  • 18
  • 148
  • 244
Arvind Chavhan
  • 488
  • 1
  • 6
  • 14