Results 1 to 6 of 6

Thread: Random number using java.util

  1. #1
    Join Date
    Dec 2009
    Posts
    178

    Random number using java.util

    Hello,
    I must create a random method that must return a number between 0 and the number in the parameter of what I read in the api java random said he refers to between 0 and 1. How can i do this? . I want to know how to using the random number using the java.util package. Also how to return a number between 1 and the number entered in parameter? Thank you in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Random number using java.util

    Hello,
    For a number between 0 and x, just multiplier the result of random by x. For a number of x to y, just multiply the result by the random y and then during recovery, to test and add x if the number is smaller than x. Alternatively you can use the method which is provided in the java.util package , but I have not used it so I can not suggest you that. But I think this makes the job, you can try this.

  3. #3
    Join Date
    Nov 2009
    Posts
    335

    Re: Random number using java.util

    Hello,
    On the sun official site that the api for the java on the sun's site, you will find something quite complete the api random. Here is the sample part of code for doing this.
    Code:
    Random r = new Random(); / / Constructor
    int i = r.nextInt(10);
    Who can do exactly what you want here an integer between 0 and 9.

  4. #4
    Join Date
    Nov 2009
    Posts
    330

    Re: Random number using java.util

    Hello,
    I think that to create a random number between x and y must:
    calculate z, the difference between x and y,, create a random number between 0 and z with rand.nextInt (z); in all cases add x to Nombe obtained. Careful to take into account the>,> =, <and <=, the calculation of the difference is not necessarily the same (sometimes z-1) and may need to add 1 x (where x is not included ).

  5. #5
    Join Date
    Nov 2009
    Posts
    583

    Re: Random number using java.util

    Hello,
    The following code show how to generate random numbers using the java.util package. This might be helpful to you. Here is the code for it
    Code:
    import java.util.Random;
    
    public final class RandomInteger {
      
      public static final void main(String... aArgs){
        
        //note a single Random object is reused here
        Random r = new Random();
        for (int in = 1; in <= 10; ++in){
          int rin = r.nextInt(100);
          log("Generated : " + rin);
        }
        
        log("Done.");
      }
      
      private static void log(String aMessage){
        System.out.println(aMessage);
      }
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    518

    Re: Random number using java.util

    Hello,
    Here is another example showing how to generate the random numbers. The above is correct , but this is a simple code and it will be easy for you to understand.
    Code:
    import java.util.Random;
    public class RandomTest {;
    
      public static void main(String[] args) {
        Random r = new Random(20071969);
    
        for (int j = 0; j<10; j++)
        {
          int p = r.nextInt(10);
          System.out.println(p);
        }
      }
    }

Similar Threads

  1. Replies: 4
    Last Post: 13-01-2012, 05:07 PM
  2. Problem using Java.util.Timer
    By Gokul20 in forum Software Development
    Replies: 7
    Last Post: 09-09-2010, 10:20 PM
  3. JRE can't see java.util.List
    By amreldeeb in forum Software Development
    Replies: 2
    Last Post: 21-02-2010, 03:31 PM
  4. Formatting time using java.util
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 03:21 AM
  5. Random number generation in java
    By GlassFish in forum Software Development
    Replies: 3
    Last Post: 16-11-2009, 11:42 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,889,767.86966 seconds with 17 queries