Results 1 to 5 of 5

Thread: Question about the nextInt() method

  1. #1
    Join Date
    Mar 2007
    Beans
    1,052

    Question about the nextInt() method

    Edit: This is a question about Java.

    Here is some code I copied (with a few added comments) from a video I'm watching (My question is about what's bolded):

    Code:
    import java.util.Random;
    
    public class learningThreads implements Runnable
    {
        String name;
        int time;
        Random r = new Random();
        
        public learningThreads(String x)
        {
            name = x;
            time = r.nextInt(999);
        }
        
        public void run()
        {
            try
            {
                System.out.printf("%s is sleeping for %d\n", name, time);
                Thread.sleep(time);
                System.out.printf("%s is done\n", name);
            }
            catch(Exception e){}
        }
        
        public static void main(String[] args)
        {
            // The following creates threads but are not yet started to be used
            Thread t1 = new Thread(new learningThreads("one"));
            Thread t2 = new Thread(new learningThreads("two"));
            Thread t3 = new Thread(new learningThreads("three"));
            Thread t4 = new Thread(new learningThreads("four"));
            
            // The following starts each thread above
            t1.start();
            t2.start();
            t3.start();
            t4.start();
        }
    
    }
    At first, I thought the purpose of the nextInt() method is to give an integer to the object that calls it but then I noticed that instead of giving it a specific integer, the integer being set is an interval with that specific integer being the end of the interval. I was just hoping someone could explain that part to me in a little more detail.

    Any input would be greatly appreciated!
    Thanks in advance!
    Last edited by s3a; May 27th, 2010 at 07:08 AM.
    Apps for Ubuntu (outdated) ---> http://cid-23a283fc1010a1bb.skydrive...%20Wine|6?uc=1
    Use Mnemosyne to Study for School!

  2. #2
    Join Date
    Feb 2009
    Beans
    789
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Question about the nextInt() method

    Yes, nextInt(n) gives a random integer in the interval 0 till and including n - 1. Full details can be found here.

  3. #3
    Join Date
    Mar 2007
    Beans
    1,052

    Re: Question about the nextInt() method

    That answered my question Also, I didn't know the integer it sends is [0,n) or [0,n-1]. Thanks a lot!
    Apps for Ubuntu (outdated) ---> http://cid-23a283fc1010a1bb.skydrive...%20Wine|6?uc=1
    Use Mnemosyne to Study for School!

  4. #4
    Join Date
    Jan 2007
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Question about the nextInt() method

    Quote Originally Posted by s3a View Post
    That answered my question Also, I didn't know the integer it sends is [0,n) or [0,n-1]. Thanks a lot!
    [0,n) or [0,n-1] is the same when you are talking about integers.

  5. #5
    Join Date
    Mar 2007
    Beans
    1,052

    Re: Question about the nextInt() method

    I know; I was just trying to say it in my own words but thanks.
    Last edited by s3a; May 28th, 2010 at 09:53 PM.
    Apps for Ubuntu (outdated) ---> http://cid-23a283fc1010a1bb.skydrive...%20Wine|6?uc=1
    Use Mnemosyne to Study for School!

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
  •