aspose file tools*
The moose likes Beginning Java and the fly likes Generating a number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generating a number" Watch "Generating a number" New topic
Author

Generating a number

Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 579
I want to generate a random number of 11 digits, i am looking for a object that wiill take a number an then generate that number of digits.

Thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11420
    
  16

I doubt there is such a thing that does what you are asking. You are going to have to write it yourself.

Do you have a specific question?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 579
Yup i want to generate random number, i know there are random number generators out there, i was just wondering of there is one that will generate numbers specic to a size given, so say give it a parameter 8 it will generate 8 numbers.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

There's not exactly a method like you describe in Java's standard API. However, it's not hard to do this, using java.util.Random.

If you want a random number with 8 digits, then basically what you want is a random number between 10,000,000 and 99,999,999.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 579
Thanks that put me in the right direction

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
That will not quite work. If you obtain a number < 1000000, you will not put 6 digits into your string builder.
And why are you using substring when there is a StringBuilder method which will reduce the output to n characters before you return it as a String?
And did you ever obtain the same printout ten times?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Tony Evans wrote:Thanks that put me in the right direction

As Campbell said, that won't work.

Possibly closer is Jesper's suggestion; but best of all: back up and tell us exactly what it is you want to do, because right now we're floundering around in the dark. I'm still not sure if you want 8 numbers, or an 11-digit number (or indeed, what an "11-digit number" means to you), or both, or some combination of either.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Earlier, I wrote:. . . If you obtain a number < 1000000, you will not put 6 digits into your string builder. . . .
Sorry. For 1000000 read 100000.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generating a number