Ad Code

Pick a Random Number in Java | How to pick a random number between minimum to maximum number in Android Studio

Pick a Random Number in Java | How to pick a random number between minimum to maximum number in Android Studio




Hello Developers, In this blog I will discuss with you how to pick a random number without using third party library. So, do not go anywhere because we always brings new contents in this blog for helping the developers in their work. Lets Start

Get Random Number
public static int getRandomNumberBetween(int min, int max) {
        Random foo = new Random();
        int randomNumber = foo.nextInt(max - min) + min;
        if (randomNumber == min) {
            // Since the random number is between the min and max values, simply add 1
            return min + 1;
        } else {
            return randomNumber;
        }
    }

Here I am giving a pubic static method, with the help of this method you can pick random number between lower to upper number. So here is the use of the method

Use random number
int randomNumber = getRandomNumberBetween(1, 100);

I do not think you need an Example project for this but I have created an Example project for you

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/randomText"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Get Random Number"/>


    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.pickrandom.number;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    Button button1;
    int minimum = 1;
    int maximum = 100;
    TextView randomText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = findViewById(R.id.button1);
        randomText = findViewById(R.id.randomText);


        button1.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View view) {

                int randomNumber = getRandomNumberBetween(minimum, maximum);

                randomText.setText(Integer.toString(randomNumber));

            }
        });




    }

    public static int getRandomNumberBetween(int min, int max) {
        Random foo = new Random();
        int randomNumber = foo.nextInt(max - min) + min;
        if (randomNumber == min) {
            // Since the random number is between the min and max values, simply add 1
            return min + 1;
        } else {
            return randomNumber;
        }
    }
}\

If you found our site helpful the bookmark this site and do not forget to visit our youtube chennal. If you are an Android App Developer then You can Download our "Project Store" Application from Google Play Store

Thank you for visiting our site

Post a Comment

0 Comments

Ad Code