0

How can I make the number in Math.random() come out no less than the number I need?

Cards card1 = new Cards();
card1.cardsnumber = Math.random()*21;
int card1int = (int) Math.round(card1.cardsnumber);

How can I make card1int come out from 2 to 21?

EuTy
  • 21
  • 1
  • 1
    `ThreadLocalRandom.nextInt( 2 , 22 )` for an `int` value between the specified origin (inclusive) and the specified bound (exclusive). See [Javadoc](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/ThreadLocalRandom.html#nextInt(int,int)). – Basil Bourque Aug 31 '22 at 23:13

1 Answers1

0

Just add 1 to the random number created and use 20 instead of 21

Cards card1 = new Cards();
card1.cardsnumber = (Math.random()*20) + 1;
int card1int = parseInt(Math.round(card1.cardsnumber))

Costas
  • 109
  • 8
  • This won't work as described. Where does parseInt come from? And you need to add 2. And just do int val = (int)(Math.random()*20) + 2; Or better use an instance of Random or ThreadLocalRandom – WJS Aug 31 '22 at 23:58