1 2 Previous Next 17 Replies Latest reply: Jun 23, 2008 7:51 AM by 800282 RSS

    Generate random int

    807601
      Hi to everyone!How do i generate random int that has length 3, i mean values from 000 to 999.Thx
        • 1. Re: Generate random int
          807601
          You STFW!, and then you find this link: http://www.cs.geneseo.edu/~baldwin/reference/random.html
          • 2. Re: Generate random int
            807601
            How do i generate random int that has length 3
            An int value doesn't have a "length". You're looking to create a String.

            ~
            • 3. Re: Generate random int
              800282
              killer8685 wrote:
              Hi to everyone!How do i generate random int that has length 3, i mean values from 000 to 999.Thx
              [http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html#nextInt(int)]
              • 4. Re: Generate random int
                800282
                yawmark wrote:
                How do i generate random int that has length 3
                An int value doesn't have a "length". You're looking to create a String.

                ~
                Of course numbers have a width: 2 is wider than 1, hence 2 > 1.
                QED

                ; )
                • 5. Re: Generate random int
                  807601
                  Of course numbers have a width: 2 is wider than 1, hence 2 > 1.
                  Ahem. That'd be width, not length.

                  QED.

                  ;oD

                  ~
                  • 6. Re: Generate random int
                    807601
                    Sorry for not explaining the problem!I want t generate a 3 digits number with values from 000 to 999.i dont want to be generated numbers with less then 3 digits!Is it possible!!???
                    • 7. Re: Generate random int
                      795426
                      the "width" of your number is purely formatting. Just generate your random number from 0 to 999 and format it using DecimalFormat or PrintStream.printf()
                      • 8. Re: Generate random int
                        807601
                        Could you help me with the decimalformatter??How to use it?I read about it but don't know how to use!
                        • 9. Re: Generate random int
                          807601
                          Try googling for "DecimalFormat tutorial" ...
                          • 10. Re: Generate random int
                            807601
                            killer8685 wrote:
                            Sorry for not explaining the problem!I want t generate a 3 digits number with values from 000 to 999.i dont want to be generated numbers with less then 3 digits!Is it possible!!???
                            Well mathematically 0, 00 and 000 is the same number. So what you need to generate is a 3-digit representation of a number. For that you can use a String because "0", "00 and "000" are different Strings.

                            The most straightforward way probably is to declare a String array holding the Strings "0" to "9". Then you can generate a random int between 0 and 9 and use as index in the array to get the corresponding String digit. Finally you concatenate three random 1-digit Strings to one 3-digit String which is what you wanted.
                            • 11. Re: Generate random int
                              807601
                              killer8685 wrote:
                              Sorry for not explaining the problem!I want t generate a 3 digits number with values from 000 to 999.i dont want to be generated numbers with less then 3 digits!Is it possible!!???
                              Just for you..
                              import java.text.DecimalFormat;
                              import java.util.Random;
                              
                              public class DecimalFormatTest {
                                   public static void main(String[] args) throws Exception {
                                        for(;;) System.out.println(new DecimalFormat("000").format(Double.valueOf(Random.class.newInstance().nextInt(999))));
                                   }
                              }
                              Enjoy.
                              • 12. Re: Generate random int
                                807601
                                prigas wrote:
                                Enjoy.
                                The call to nextInt(999) produces random numbers between 0 and 998, so it should be nextInt(1000).

                                Also Random.class.newInstance() starts a new random sequence each time it's called (and the first number of that sequence is picked). It's questionable whether a sequence produced in this way has the same statistical quality as when the numbers are from the same random sequence.
                                • 13. Re: Generate random int
                                  807601
                                  System.out.printf("%03d\n:", num); // where num is an integer with value from 0 to 999

                                  Also enjoy!

                                  Alan.
                                  • 14. Re: Generate random int
                                    807601
                                    prigas you are the best!Exactly what i was looking for!You are my lifesaver......................THX THXTHXTHXTHX
                                    The question is answered!
                                    1 2 Previous Next