Generating a Random Number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vrkamalakar
    New Member
    • Jan 2007
    • 16

    #1

    Generating a Random Number

    Hi,

    I need a piece of code to generate a random number between 0 and 5 (both exclusive) ie., number should be either 1,2,3 or 4. And also I need only two unique random numbers out of the four numbers. ie., out of 1,2,3 and 4 only two numbers should be picked randomly and those two numbers should not be repeated. Any help please....

    Regards,
    VRK
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by vrkamalakar
    Hi,

    I need a piece of code to generate a random number between 0 and 5 (both exclusive) ie., number should be either 1,2,3 or 4. And also I need only two unique random numbers out of the four numbers. ie., out of 1,2,3 and 4 only two numbers should be picked randomly and those two numbers should not be repeated. Any help please....

    Regards,
    VRK
    Did you manage to use Math.random() as was suggested here?

    Comment

    • vrkamalakar
      New Member
      • Jan 2007
      • 16

      #3
      Hi,

      I got it....Thanks a lot...


      Originally posted by r035198x
      Did you manage to use Math.random() as was suggested here?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by vrkamalakar
        Hi,

        I got it....Thanks a lot...
        All you need is to get the first number, then use a while loop that generates another number until the generated number is different from the first number.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Alternatively, you could use an ArrayList (found in java.util.Array List) to build a short list of integers (from 1 to 4, in your case). Then, using a random number generator, generate a number between 0 and the size of your ArrayList, select that Integer from the List, and remove the Integer object. This way, you are guaranteed never to produce the same Integer, as it will be removed after you use it.

          For a larger application, this is wonderful, but for 4 possible numbers, it may take too much time, memory space, coding, etc.

          Comment

          • vrkamalakar
            New Member
            • Jan 2007
            • 16

            #6
            Hi,
            Can you give me a piece of code for that. With the earlier method Iam getting even 0 as a random number which is not my requirement. I should get either 1,2 3 or 4.


            Originally posted by Ganon11
            Alternatively, you could use an ArrayList (found in java.util.Array List) to build a short list of integers (from 1 to 4, in your case). Then, using a random number generator, generate a number between 0 and the size of your ArrayList, select that Integer from the List, and remove the Integer object. This way, you are guaranteed never to produce the same Integer, as it will be removed after you use it.

            For a larger application, this is wonderful, but for 4 possible numbers, it may take too much time, memory space, coding, etc.

            Comment

            • vrkamalakar
              New Member
              • Jan 2007
              • 16

              #7
              Hi,
              Thanks....I got it.....


              Originally posted by vrkamalakar
              Hi,
              Can you give me a piece of code for that. With the earlier method Iam getting even 0 as a random number which is not my requirement. I should get either 1,2 3 or 4.

              Comment

              Working...