Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to eliminate 0 from possibilities in the Random class?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to eliminate 0 from possibilities in the Random class?

    Hey guys. I'm working in some exercises and one of them tells me I need to program a code where I generate a die where I get values between 1 and 6.

    This is what I have:
    import java.util.Random;
     
    public class RandomGenerator 
    {
    	public static void main(String[] args)
    	{
    		Random generator = new Random();
    		System.out.println(generator.nextInt(7));
    		//How can I eliminate 0 from the answers?
    	}
    }

    It works out well except that I want to eliminate 0 from the answers and don't know how. Could anyone help?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: How to eliminate 0 from possibilities in the Random class?

    What possible range of results will you get if you use generator.nextInt(6)?

    Now knowing this, what can you do to the int that you get from generator.nextInt(6) to have it lie within your desired range?

  3. #3
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to eliminate 0 from possibilities in the Random class?

    Quote Originally Posted by curmudgeon View Post
    What possible range of results will you get if you use generator.nextInt(6)?

    Now knowing this, what can you do to the int that you get from generator.nextInt(6) to have it lie within your desired range?
    I get a range of values between 0 and 5.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: How to eliminate 0 from possibilities in the Random class?

    Quote Originally Posted by jean28 View Post
    I get a range of values between 0 and 5.
    Right, and so as you mention you get an int between 0 and 5; so what can you do to this int so that its range is between 1 and 6?

  5. #5
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to eliminate 0 from possibilities in the Random class?

    Add 1. I feel so dumb now thanks.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: How to eliminate 0 from possibilities in the Random class?

    Quote Originally Posted by jean28 View Post
    Add 1. I feel so dumb now thanks.
    No, not dumb, just new. I knew you could figure it out. Well done.

  7. The Following User Says Thank You to curmudgeon For This Useful Post:

    jean28 (September 23rd, 2012)

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 04:46 PM
  2. random not working in .class method
    By Spidey1980 in forum Java SE APIs
    Replies: 13
    Last Post: August 19th, 2011, 07:35 AM
  3. Using Random class
    By javapenguin in forum What's Wrong With My Code?
    Replies: 22
    Last Post: November 18th, 2010, 01:34 PM
  4. How to eliminate Junk charaters from the file...
    By Harry_ in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 10th, 2009, 09:52 AM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM