mishalhaneef
mishalhaneef

Reputation: 852

How to generate random numbers within a limit in dart

I need to generate random numbers within a limit.

Random random = new Random.secure();
int randomNumber = random.nextInt(100);

this will generate the random number within 100. but I want to generate within 50 to 100

how do I do that?

Upvotes: 0

Views: 566

Answers (1)

Teroaz
Teroaz

Reputation: 300

Just generate a random number between 0 and 50 and then add 50, to make it between 50 and 100.

Random random = new Random.secure();
int randomNumber = random.nextInt(51) + 50;

Don't forget that the number passed to the method nextInt is excluded, so it will generate a number between 0 and 49 if you pass 50, instead you should pass 51.

Upvotes: 2

Related Questions