কম্পিউটার টিউটোরিয়াল

এন্ড্রয়েডে নির্দিষ্ট পরিসরে র‍্যান্ডম সংখ্যা জেনারেট করার পদ্ধতি

এন্ড্রয়েডে নির্দিষ্ট পরিসরে র‍্যান্ডম সংখ্যা জেনারেট করার ধাপাবলী

এই উদাহরণটি দেখায় কীভাবে java.util.Random ক্লাস ব্যবহার করে ব্যবহারকারীর দেওয়া ন্যূনতম এবং সর্বোচ্চ মানের মধ্যে র‍্যান্ডম সংখ্যা তৈরি করা যায়।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।

ধাপ ২: লেআউট ফাইল সংশোধন করুন (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn_generate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_max"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:text="জেনারেট করুন" />
    <EditText
        android:id="@+id/et_min"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:hint="ন্যূনতম"
        android:inputType="number"/>
    <EditText
        android:id="@+id/et_max"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/et_min"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:hint="সর্বোচ্চ"
        android:inputType="number"/>
    <TextView
        android:id="@+id/tv_output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn_generate"
        android:textSize="30dp"
        android:layout_centerHorizontal="true"
        android:hint="আউটপুট"
        android:text="" />
</RelativeLayout>

ধাপ ৩: মূল এক্টিভিটি লজিক (src/MainActivity.java)

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
    EditText et_min, et_max;
    Button btn_generate;
    TextView tv_output;
    Random r;
    int min, max, output;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        r = new Random();
        et_min = findViewById(R.id.et_min);
        et_max = findViewById(R.id.et_max);
        btn_generate = findViewById(R.id.btn_generate);
        tv_output = findViewById(R.id.tv_output);
        
        btn_generate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tempMin = et_min.getText().toString();
                String tempMax = et_max.getText().toString();
                
                if (!tempMin.isEmpty() && !tempMax.isEmpty()) {
                    min = Integer.parseInt(tempMin);
                    max = Integer.parseInt(tempMax);
                    
                    if (max > min) {
                        // nextInt(bound) 0 থেকে bound-1 পর্যন্ত দেয়, তাই +1 এবং +min
                        output = r.nextInt((max - min) + 1) + min;
                        tv_output.setText(String.valueOf(output));
                    }
                }
            }
        });
    }
}

ধাপ ৪: ম্যানিফেস্ট ফাইল (AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

র‍্যান্ডম সংখ্যা জেনারেশনের তর্ক

r.nextInt((max - min) + 1) + min ফর্মুলাটি নিশ্চিত করে যে ফলাফল min এবং max (উভয়ই অন্তর্ভুক্ত) এর মধ্যে পড়ে।

অ্যাপ্লিকেশন চালান

আপনার আসল এন্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে অনুমান করি। Android Studio-তে আপনার প্রজেক্টের এক্টিভিটি ফাইল খুলুন এবং টুলবারের Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস নির্বাচন করুন এবং আপনার ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।

  1. অ্যান্ড্রয়েড ডিভাইসের সিরিয়াল নম্বর বের করার পদ্ধতি

  2. Android-এ URL এনকোড করার পদ্ধতি

  3. Android Switch উইজেট কাস্টমাইজ করার পদ্ধতি (স্টাইলিং)

  4. Android-এ ফোন নম্বর আনব্লক করার ধাপে ধাপে গাইড