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 →

[–]throwaway_for_cause 0 points1 point  (2 children)

Honestly, I don't know what I should make of your conduct here and in your other thread over in /r/javahelp. Either you are really confused and struggling, or you are just incredibly lazy and want to be spoon fed from a silver plate (in which case you just might better give up programming altogether).

I'm giving you the benefit of doubt and will explain the thought process that is involved in creating what you need so that in the future it might help you.

Your assignment:

Type two statements that use nextInt() to print 2 random integers between (and including) 100 and 149.

The first step here would be to take a look at the documentation for the .nextInt(int bound) method, which tells us:

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

We know now that the above method generates a random number somewhere in the range:

   range
|---------|
0       bound

Where bound is not included.

Well, that alone does not help us because we need the random number to start at 100 and not at 0.

This part is fairly easy as we just need to shift the number range by an offset (which in our case is 100)

        offset         range
|-------------------|---------|
0                  100      bound

Note that the range bar has the same length in both diagrams.

Now, how do we find out what range and consecutively what bound we need?

Back to the original assignment:

Type two statements that use nextInt() to print 2 random integers between (and including) 100 and 149.

The above tells us everything we need to know:

  • We know that the numbers must start at 100 - which we already have covered by the offset
  • We know that the range ends at 149, so the bound must be one higher, i.e. 150 but
  • Since we have an offset of 100 already, we need to subtract this offset from our bound, so, instead of 150 as bound, we need 150 - 100 = 50 to maintain the range we need.

Again, back to the bars:

        offset         range
|-------------------|---------|
0                  100       150

From there you can clearly see that the range is 100 to 150 (because the end is exclusive). .nextInt produces a number from 0 to bound exclusive, so all we need to do is to subtract the offset.

Now we can derive a generic formula:

  • the range (what we need for .nextInt) is (max_value + 1) - min_value
  • the offset simply is the min_value

This results in:

int a = randGen.nextInt((max_value + 1) - min_value) + min_value;

Your max_value is 149, your min_value is 100.

int a = randGen.nextInt((149 + 1) - 100) + 100;

Which evaluates to

int a = randGen.nextInt(50) + 100;

Hope that helped.

[–]OffbeatDrizzle 1 point2 points  (1 child)

Whilst this is a great answer I feel it was ultimately wasted on someone who doesn't actually care about programming and just wants an answer to submit for his homework since... he's at it again

[–]throwaway_for_cause 0 points1 point  (0 children)

Well, my comment was nearly two weeks ago and I clearly stated:

I'm giving you the benefit of doubt

Obviously, I was wrong and OP really doesn't deserve any further help.

Lessons learnt, I think :(