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

কোটলিন ব্যবহার করে অ্যান্ড্রয়েডে নির্দিষ্ট রেঞ্জে র‍্যান্ডম নম্বর জেনারেট কীভাবে করবেন?

এই উদাহরণটি প্রদর্শন করে কীভাবে অ্যান্ড্রয়েডে কোটলিন ব্যবহার করে নির্দিষ্ট রেঞ্জে র‍্যান্ডম নম্বর জেনারেট করা যায়।

পদক্ষেপ ১: নতুন প্রজেক্ট তৈরি করুন

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"> 
    <TextView 
        android:id="@+id/text" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" 
        android:layout_marginTop="50dp" 
        android:text="Tutorials Point" 
        android:textAlignment="center" 
        android:textColor="@android:color/holo_green_dark" 
        android:textSize="32sp" 
        android:textStyle="bold" /> 
    <EditText 
        android:id="@+id/editTextMin" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/text" 
        android:layout_centerInParent="true" 
        android:layout_marginTop="50dp" 
        android:ems="10" 
        android:hint="Minimum" 
        android:inputType="number" /> 
    <EditText 
        android:id="@+id/editTextMax" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/editTextMin" 
        android:layout_centerHorizontal="true" 
        android:ems="10" 
        android:hint="Maximum" 
        android:inputType="number" /> 
    <Button 
        android:id="@+id/btn_generate" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/editTextMax" 
        android:layout_centerInParent="true" 
        android:layout_marginTop="5dp" 
        android:text="GENERATE" /> 
    <TextView 
        android:id="@+id/textViewResult" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/btn_generate" 
        android:layout_centerInParent="true" 
        android:layout_marginTop="10dp" 
        android:hint="Output" 
        android:text="" 
        android:textColor="@android:color/black" 
        android:textSize="24sp" 
        android:textStyle="bold" /> 
</RelativeLayout>

পদক্ষেপ ৩: src/MainActivity.kt-এ কোড যোগ করুন

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.util.Random

class MainActivity : AppCompatActivity() {
    private lateinit var editTextMin: EditText
    private lateinit var editTextMax: EditText
    private lateinit var button: Button
    private lateinit var textView: TextView
    private val random = Random()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        editTextMin = findViewById(R.id.editTextMin)
        editTextMax = findViewById(R.id.editTextMax)
        button = findViewById(R.id.btn_generate)
        textView = findViewById(R.id.textViewResult)

        button.setOnClickListener {
            val minStr = editTextMin.text.toString()
            val maxStr = editTextMax.text.toString()

            if (minStr.isNotEmpty() && maxStr.isNotEmpty()) {
                val min = minStr.toInt()
                val max = maxStr.toInt()

                if (max >= min) {
                    // [min, max] রেঞ্জে র‍্যান্ডম নম্বর
                    val output = random.nextInt(max - min + 1) + min
                    textView.text = output.toString()
                } else {
                    textView.text = "Max must be >= Min"
                }
            } else {
                textView.text = "Enter both values"
            }
        }
    }
}
কোটলিন স্ট্যান্ডার্ড লাইব্রেরি বিকল্প:
// Kotlin 1.3+ Range ব্যবহার
val output = (min..max).random()

// ThreadLocalRandom (জাভা ৭+)
import java.util.concurrent.ThreadLocalRandom
val output = ThreadLocalRandom.current().nextInt(min, max + 1)

পদক্ষেপ ৪: androidManifest.xml-এ কোড যোগ করুন

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="https://schemas.android.com/apk/res/android" 
    package="com.example.q11"> 
    <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>

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

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

  1. জাভাস্ক্রিপ্টে নির্দিষ্ট রেঞ্জে র‍্যান্ডম পূর্ণসংখ্যা কীভাবে জেনারেট করবেন?

  2. Android SQLite-এ RANDOM() ফাংশন কীভাবে ব্যবহার করবেন?

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

  4. পাইথনে সংখ্যা দিয়ে পিরামিড প্যাটার্ন জেনারেট করার প্রোগ্রাম