1

I've got two Integer variables that are generated randomly

int scoreWon = random1.nextInt(20);
int scoreLost = random1.nextInt();

What I want to do is , bound the scoreLost value by scoreWon so the generated number will always be less than the value generated by scoreWon.

Is this way possible ?

int scoreWon = random1.nextInt(20);
int scoreLost = random1.nextInt(scoreWon);

or please help me out

Bashir
  • 2,057
  • 5
  • 19
  • 44
kavx
  • 67
  • 1
  • 8
  • Why not try it out and see what happens? – Nico Haase Dec 16 '20 at 17:22
  • Does this answer your question? [How do I generate random integers within a specific range in Java?](https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java) – NoOneIsHere Dec 16 '20 at 19:39

2 Answers2

0

Yes, this is possible. Just try it. The int parameter for nextInt() is the upper bound (exclusively) for the new random integer value (the lower bound is 0, inclusively).

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
0

Yes that's probably the best approach. The Random.nextInt(int bound) method returns a number from 0 to bound-1 (bound is exclusive) as described here:

Random.nextInt documentation

Example: Random.nextInt(100) would be 0...99

In your context:

Random random = new Random();
int scoreWon = random.nextInt(20); //0...19
int scoreLost = random.nextInt(scoreWon); //0...18