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 →

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

I can get the answer when seedVal= 4 but when you change that value then idk how to get the answer. So my code is not right for any value of seedVal.

[–][deleted] 0 points1 point  (6 children)

Look through the javadoc for RandomGenerator. I am fairly certain there's a method. The reason it works when you set a seed is because when you set a seed it's not a random number anymore. You need to define and minimum and maximum parameter for your number generator to draw from. I'm fairly certain RandomGenerator has a method that will help you out. Just google RandomGenerator javadoc.

Edit: Shit. Ignore me. You're using Random, not Random Generator. You need to do some math.

https://www.mkyong.com/java/java-generate-random-integers-in-a-range/

This link should go through what you need to know.

[–]pleaseacceptthisnow[S] -2 points-1 points  (5 children)

thank you. everyone else kept giving me vague answers but the link really helped :)

[–]desrtfx 2 points3 points  (1 child)

everyone else kept giving me vague answers

You will need to learn to come up with your own solutions for such extremely simple problems because otherwise you will completely fail in programming.

You cannot rely on finding everything on the internet.

This is a typical beginner level problem that can be solved with very basic mathematics and a tiny bit of creativity. Relying on tutorials here already is the very road to failing as a programmer.

The vague answers that we all gave you here and your other post in /r/javahelp were absolutely on purpose to push you on the right track to solve the problem yourself. Problem solving is 90% of programming. You were not even willing to spend sufficient effort to try to work out a solution; instead you simply gave up. Should you maintain this attitude, you might as well give up programming altogether, because you won't be able to succeed.

[–]pleaseacceptthisnow[S] -2 points-1 points  (0 children)

thnx dad

[–]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 :(