4

Possible Duplicate:
Java random number with given length

I have been trying to generate a 15 digit long number in java but it seems I have failed to do it so far as using:

This is producing a maximum of 10 digits.

  Random random = new Random();
  int rand15Digt = random.nextInt(15);

How can I generate it successfully?

Community
  • 1
  • 1
guthik
  • 404
  • 1
  • 7
  • 15
  • oh i forgot to add that i used a number that is a power of ten as the seed to the random generator. just to make sure that i have the required number of digits. – guthik Dec 14 '12 at 09:58
  • 1
    the seed of random won't affect the number of digits of your output – bowmore Dec 14 '12 at 10:02
  • 2
    Your current code produces an `int` between 0 and 15. Are you looking to produce a number that has fifteen characters when represented in decimal? I.e. something between `100,000,000,000,000` and `999,999,999,999,999`? – Duncan Jones Dec 14 '12 at 10:02
  • 3
    http://stackoverflow.com/questions/5392693/java-random-number-with-given-length – Thihara Dec 14 '12 at 10:06
  • yes, exactly something like that. – guthik Dec 14 '12 at 10:09
  • This has nothing to do with [tag:swing], [tag:javascript] *or* [tag:java-ee]. Please choose tags more carefully – Andrew Thompson Dec 14 '12 at 10:12
  • yeah, about the tags: sorry about that but the question wasn't accepted till i added them - it kept on saying "Ooops, question doesn't meet our requirements" or something like that. still sorry for that. – guthik Dec 14 '12 at 10:17
  • like i said above. i added about the power thing, it works for seeds 10 and below. above that, it goes berserk and gives wierd results. – guthik Dec 14 '12 at 10:26

5 Answers5

5

Use Random's method public long nextLong()

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
StanislavL
  • 56,971
  • 9
  • 68
  • 98
4

To begin with, an int can hold numbers between -2,147,483,648 and 2,147,483,647.

Use Random.nextLong()

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • 1
    @DuncanJones, 000000000000012 is a `15 digit result`. The OP has not asked for ("a number between 10+e15 and 10+e16-1"). – SJuan76 Dec 14 '12 at 10:10
  • Fair point! I'll remove my downvote. – Duncan Jones Dec 14 '12 at 10:19
  • Still i dont get how can ensure that my result is a 15 digits long. .nextLong() is yeilding values greater longer than 15 digits so far. – guthik Dec 14 '12 at 10:24
  • @user1825142 You need to be specific about what you want. As SJuan76 states, any number can be considered *x*-digits long if you format it as such. If you are looking for a specific number range, make it clear in the question. – Duncan Jones Dec 14 '12 at 10:25
  • 1
    @DuncanJones - well consider it to be from 100000000000000 to 999999999999999 – guthik Dec 14 '12 at 10:39
  • @SJuan76 Apologies for the quick-draw -1. I've made a tiny edit to your answer to allow me to give a +1. – Duncan Jones Dec 14 '12 at 10:43
4

Any number can be formatted into 15 decimal digits when presented as a string. This is achieved when the number is converted to a String, e.g.:

System.out.println(String.format("%015d", 1));

// prints: 000000000000001

If you want to generate a random number that lies between 100,000,000,000,000 and 999,999,999,999,999 then you can perform a trick such as:

Random random = new Random();    
long n = (long) (100000000000000L + random.nextFloat() * 900000000000000L);

If your ultimate goal is to have a 15-character string containing random decimal digits, and you're happy with third-party libraries, consider Apache commons RandomStringUtils:

boolean useLetters = false;
boolean useNumbers = true;
int stringLength = 15;

String result = RandomStringUtils.random(stringLength, useLetters, useNumbers) 
Community
  • 1
  • 1
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • 1
    -1: The OP has asked for a number, not a String. – SJuan76 Dec 14 '12 at 10:12
  • ok, let me download this and try it. i'll get back to you ASAP. – guthik Dec 14 '12 at 10:14
  • @SJuan76 Touché :-) I made the assumption he was going to be using a string eventually, but I could well be wrong. – Duncan Jones Dec 14 '12 at 10:21
  • @Duncan Jones; how do i add this RandomStringUtils library to my code? or where can i get it from. apparently its already there somewhere but i cant download it or import it, or directly use it. – guthik Dec 14 '12 at 10:33
  • @Duncan Jones - you are right as well. i will use it as a string eventually. so how can i get that to work. i mean add to my code. – guthik Dec 14 '12 at 10:48
  • @user1825142 You need to [download commons lang](http://commons.apache.org/lang/download_lang.cgi). Or just use the trick I added to the question above. – Duncan Jones Dec 14 '12 at 10:49
  • @SJuan76 The question is long since dead, but I've edited my answer to reflect the different options available. – Duncan Jones Dec 14 '12 at 10:49
  • 1
    @Duncan Jones - thanks alot. that quite did the trick :)!! – guthik Dec 14 '12 at 10:55
  • Fair is fair. I was a quick downvoter in my young years, too :-) – SJuan76 Dec 14 '12 at 20:02
1

What about trying BigInteger, see this StackOverflow link for more information Random BigInteger Generation

Community
  • 1
  • 1
Rahul
  • 271
  • 2
  • 9
0

In Hibernate have UUIDGenerator class using this we can create Secure Random and Unique Number also.

public Serializable generate(SessionImplementor session, Object object)
        throws HibernateException {
    String uuid = (String)super.generate(session, object);
    return uuid.substring(uuid.length()-15, uuid.length());
}

i think this is best way to Generate Unique and also Random Number...

Java Developer
  • 1,873
  • 8
  • 32
  • 63
  • Thanks but i have tried and i can't get this to work. its creating alot of both run time and compilation errors. – guthik Dec 14 '12 at 10:45