If you need to generate Random numbers in your Android app (assuming you’re using Java), you can simply use the java.util.Random class.

Random random = new Random();        // (1)
mrandomnumber = random.nextInt(100); // (2)

(1) Creates a new Random object

(2) Creates a new random integers not greater than 100

Java’s Random class generates not only integers, it can also handle double, float, boolean etc., so, make sure you check out the docs at java.util.Random docs page

Here’s a small sample on how to use the Random class in Java. The RandomNumber class has two methods named getNumber() and createRandomNumber(), it’s supposed to behave like a Singleton so, it’s designed to create a random number only once.

import android.util.Log;
import java.util.Random;

public class RandomNumber  {

  private String TAG = getClass().getSimpleName();

  int mrandomnumber;
  boolean minitialized = false;

  String getNumber() {
    if(!minitialized) {
    Log.i(TAG, "RETURN Random number");
    return mrandomnumber + "";

   void createRandomNumber() {
    Log.i(TAG, "CREATE NEW Random number");
    Random random = new Random();
    mrandomnumber = random.nextInt(100);
    minitialized = true;

Then, you can use it like this from MainActivity

public class MainActivity extends AppCompatActivity {

  protected void onCreate(Bundle savedInstanceState) {

    RandomNumber data = new RandomNumber();

    ((TextView) findViewById(R.id.txtrandom)).setText(data.getNumber());
Learn Android Studio 3
Learn Android Studio 3

Learn more. See more code samples like this

Get the book