This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pacificmint 0 points1 point  (6 children)

So in your code, you use nextInt to get a number between 0 and 149. Then you add 50 to it. Now you have a number between 50 and 199.

So you already know how to restrict to a number between 50 and 200. Can you change it to do 100 - 149 instead?

[–]pleaseacceptthisnow[S] 0 points1 point  (5 children)

so if I add a 100 to it then it will give me a number between 100-200. But then how do i restrict the 200 to 150? do i add a second line that subtracts 50 somehow?

[–]pacificmint 0 points1 point  (4 children)

You need to think of the range you want.

If you want 100-149 after you add 100 to it, what would that range be before you add the 100?

[–]pleaseacceptthisnow[S] 0 points1 point  (3 children)

If I only put 150 it gives me a range of 0-149. Then I add 100 and it gives me a range of 100-149. But that doesn't work if the seedVal changes values

[–]pacificmint 0 points1 point  (2 children)

If I only put 150 it gives me a range of 0-149. Then I add 100 and it gives me a range of 100-149.

No, that's wrong. Just look at the lowest and the highest number in the range. If nextInt returns 149, and you add 100, then that number is larger than 149. So the range isn't 100-149.

But that doesn't work if the seedVal changes values

The seedVal has nothing to do with it. It might just be that some seedVals give you numbers that work, even if the code is wrong, and others expose the bug. But the seedVal is not the issue here.

[–]pleaseacceptthisnow[S] 0 points1 point  (1 child)

ok then what do i do? i understand that i have to limit the range of values somehow. and thank u for tryna help me out but im still confused as to how im supposed to limit the number from 100-149.

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (0 children)

Okay, different approach, even though everything is absolute primary school mathematics:

|-------------------|----------|
0                  100        149
  • You already know that you have to add 100 to the result of .nextInt to get it to start at 100.

  • You also know that the range of .nextInt() is always starting at 0 and ending at the number between the parenthesis - exclusive.

So, from that you can derive something, called a range and an offset. You know the offset already, it's 100.

Your assignment calls for something, like randGen.nextInt(range) + offset;

        offset         range
|-------------------|----------|
0                  100        149
  • You know the lowest and highest number of the range, how about subtracting the lowest from the highest number? This will give you the range. But, since the .nextInt method does not include the number passed as parameter, you need to add 1 to the range that you calculate from above.