Random Number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newgal
    New Member
    • Jan 2008
    • 8

    Random Number

    Hi,

    I am wondering if I create a random number

    Random random = new Random()

    Is there a way to print out the random number?

    I tried to System.out.prin tln("random number = " + random);

    and it printout "random number = java.util.Rando m@9304b1"
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Calling the Random() method like that does not give you a random number, it gives you an object of the Random class, whose toString() seems to be java.util.Rando m@<its address in memory>. To actually get a random number, call the class's methods, next() or nextInt() are probably what you want.

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      Originally posted by newgal
      Hi,

      I am wondering if I create a random number

      Random random = new Random()

      Is there a way to print out the random number?

      I tried to System.out.prin tln("random number = " + random);

      and it printout "random number = java.util.Rando m@9304b1"


      You can also directly use
      Code:
      new Random().nextInt(integer);

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by sukatoa
        You can also directly use
        Code:
        new Random().nextInt(integer);
        That's great until you want another one ;-)

        Comment

        • sukatoa
          Contributor
          • Nov 2007
          • 539

          #5
          Originally posted by BigDaddyLH
          That's great until you want another one ;-)
          Ohh!! I forgot, Codes are restricted.. hehehehhh OK!! But i could not change it anymore... Next time i will be careful...

          BigDaddy, i have a thread there Proper Invoking .COM file, can you advice me for that? That is the last part of my program...
          Last edited by sukatoa; Feb 26 '08, 12:26 AM. Reason: changing

          Comment

          • newgal
            New Member
            • Jan 2008
            • 8

            #6
            Thanks, random.nextInt( ) gives a printout.

            If a number is seeded,
            Random random = new Random(10)
            I find that it also does not print the given seed. Is it because it's also not called from the method? So the real seed is not 10, but someother number right?

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by newgal
              Thanks, random.nextInt( ) gives a printout.

              If a number is seeded,
              Random random = new Random(10)
              I find that it also does not print the given seed. Is it because it's also not called from the method? So the real seed is not 10, but someother number right?
              I'm note sure you know what the seed is. It's just a value used for the pseudo random number generator's internal state. It is not the value that nextInt will return on its next invocation, for example. As the API states:

              The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int).

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Originally posted by sukatoa
                BigDaddy, i have a thread there Proper Invoking .COM file, can you advice me for that? That is the last part of my program...
                Sorry, I'm not one for going outside of Java...

                Comment

                • newgal
                  New Member
                  • Jan 2008
                  • 8

                  #9
                  Got ya! Thanks very much.

                  Comment

                  • Doegon
                    New Member
                    • Feb 2008
                    • 14

                    #10
                    Originally posted by newgal
                    Hi,

                    I am wondering if I create a random number

                    Random random = new Random()

                    Is there a way to print out the random number?

                    I tried to System.out.prin tln("random number = " + random);

                    and it printout "random number = java.util.Rando m@9304b1"
                    yes there is,but you need to understand that after creating the random number generator object you should use its methods to get a random nuber and store it in a variable/identifier then print the variable.

                    what you did is try to print the object so since random is an object it holds the address referencing the object so @9304b1 is the address of the object

                    fing the random class API and you will get the methods,good luck...hope it helps

                    Comment

                    Working...