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

• September 22nd, 2012, 02:51 PM
jean28
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:
Code :

```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?
• September 22nd, 2012, 02:56 PM
curmudgeon
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?
• September 22nd, 2012, 02:58 PM
jean28
Re: How to eliminate 0 from possibilities in the Random class?
Quote:

Originally Posted by curmudgeon
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.
• September 22nd, 2012, 03:13 PM
curmudgeon
Re: How to eliminate 0 from possibilities in the Random class?
Quote:

Originally Posted by jean28
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?
• September 22nd, 2012, 03:15 PM
jean28
Re: How to eliminate 0 from possibilities in the Random class?
Add 1. I feel so dumb now :rolleyes: thanks.
• September 22nd, 2012, 04:52 PM
curmudgeon
Re: How to eliminate 0 from possibilities in the Random class?
Quote:

Originally Posted by jean28
Add 1. I feel so dumb now :rolleyes: thanks.

No, not dumb, just new. I knew you could figure it out. Well done.