Random Number with Limit in android

Frequently used code samples and tutorials
vivekkalady
Site Admin
Posts: 35
Joined: Tue Nov 18, 2014 2:39 pm

Random Number with Limit in android

Postby vivekkalady » Sat Nov 22, 2014 3:25 pm

Random Number is generated using Random Class

simple code block to show Random number

Code: Select all

int pos = rand.nextInt(100);

this is the simple function which returns a random number

The code below gives the random number between 20 and 90

Code: Select all

int min = 20;
int max = 90;

Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;


nextInt(int max) returns an int between 0 inclusive and max exclusive. so we have to use +1

Return to “Tutorials & Examples”

Who is online

Users browsing this forum: No registered users and 1 guest

cron