Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड में कोटलिन का उपयोग करके दी गई सीमा में यादृच्छिक संख्या कैसे उत्पन्न करें?

कोटलिन में रेंज में यादृच्छिक संख्या उत्पन्न करना

यह उदाहरण दर्शाता है कि Random क्लास का उपयोग करके निर्दिष्ट न्यूनतम और अधिकतम मानों के बीच यादृच्छिक पूर्णांक कैसे उत्पन्न किया जाए।

चरण 1: नया प्रोजेक्ट बनाएं

एंड्रॉइड स्टूडियो में File → New Project चुनें।

चरण 2: लेआउट (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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"
    android:orientation="vertical"
    android:padding="24dp"
    android:gravity="center"
    tools:context=".MainActivity"> 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="यादृच्छिक संख्या जनरेटर"
        android:textSize="24sp"
        android:textStyle="bold"
        android:textColor="@android:color/holo_green_dark"
        android:layout_marginBottom="32dp" /> 
    
    <EditText
        android:id="@+id/editTextMin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="न्यूनतम मान"
        android:inputType="number|numberSigned"
        android:textAlignment="center"
        android:layout_marginBottom="12dp" /> 
    
    <EditText
        android:id="@+id/editTextMax"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="अधिकतम मान"
        android:inputType="number|numberSigned"
        android:textAlignment="center"
        android:layout_marginBottom="24dp" /> 
    
    <Button
        android:id="@+id/btnGenerate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="यादृच्छिक संख्या उत्पन्न करें"
        android:textSize="18sp"
        android:layout_marginBottom="24dp" /> 
    
    <TextView
        android:id="@+id/textViewResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="परिणाम: -"
        android:textSize="36sp"
        android:textStyle="bold"
        android:textColor="@android:color/holo_blue_dark" /> 
        
    <TextView
        android:id="@+id/textViewInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="सीमा: -"
        android:textSize="14sp"
        android:textColor="@android:color/darker_gray" /> 
</LinearLayout>

चरण 3: MainActivity.kt

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlin.random.Random

class MainActivity : AppCompatActivity() {
    private lateinit var editTextMin: EditText
    private lateinit var editTextMax: EditText
    private lateinit var btnGenerate: Button
    private lateinit var textViewResult: TextView
    private lateinit var textViewInfo: TextView
    
    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)
        btnGenerate = findViewById(R.id.btnGenerate)
        textViewResult = findViewById(R.id.textViewResult)
        textViewInfo = findViewById(R.id.textViewInfo)
        
        btnGenerate.setOnClickListener { generateRandomNumber() }
    }
    
    private fun generateRandomNumber() {
        val minStr = editTextMin.text.toString().trim()
        val maxStr = editTextMax.text.toString().trim()
        
        // इनपुट सत्यापन
        if (minStr.isEmpty() || maxStr.isEmpty()) {
            Toast.makeText(this, "कृपया न्यूनतम और अधिकतम दोनों मान दर्ज करें", Toast.LENGTH_SHORT).show()
            return
        }
        
        val min = minStr.toIntOrNull()
        val max = maxStr.toIntOrNull()
        
        if (min == null || max == null) {
            Toast.makeText(this, "कृपया मान्य पूर्णांक दर्ज करें", Toast.LENGTH_SHORT).show()
            return
        }
        
        if (min > max) {
            Toast.makeText(this, "न्यूनतम मान अधिकतम से बड़ा नहीं हो सकता", Toast.LENGTH_SHORT).show()
            return
        }
        
        // यादृच्छिक संख्या उत्पन्न करें (सम्मिलित सीमा)
        val randomNumber = Random.nextInt(min, max + 1)
        
        // परिणाम प्रदर्शित करें
        textViewResult.text = "परिणाम: $randomNumber"
        textViewInfo.text = "सीमा: $min से $max (सम्मिलित)"
    }
}

चरण 4: AndroidManifest.xml

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

कोटलिन में यादृच्छिक संख्या उत्पन्न करने के विभिन्न तरीके:

import kotlin.random.Random

// 1. सम्मिलित सीमा (min से max तक) - अनुशंसित
val randomInt = Random.nextInt(min, max + 1)  // max सम्मिलित
val randomInt = Random.nextInt(min, max)      // max अपवर्जित (कोटलिन 1.3+)

// 2. 0 से max-1 तक
val randomInt = Random.nextInt(max)

// 3. Long रेंज
val randomLong = Random.nextLong(min, max)

// 4. Double (0.0 से 1.0)
val randomDouble = Random.nextDouble()
val randomDoubleInRange = Random.nextDouble(min.toDouble(), max.toDouble())

// 5. Boolean
val randomBool = Random.nextBoolean()

// 6. संग्रह से यादृच्छिक तत्व
val list = listOf("सेब", "केला", "चेरी", "खजूर")
val randomItem = list.random()

// 7. रेंज से यादृच्छिक
val randomFromRange = (1..100).random()

// 8. बीज के साथ (पुनरुत्पादन योग्य परिणाम)
val seededRandom = Random(12345L)
val predictable = seededRandom.nextInt(1, 100)

// 9. क्रिप्टोग्राफिक रूप से सुरक्षित (सुरक्षा के लिए)
import java.security.SecureRandom
val secureRandom = SecureRandom()
val secureInt = secureRandom.nextInt(max - min + 1) + min

यादृच्छिक संख्या जनरेटर की तुलना:

जनरेटरथ्रेड-सुरक्षितक्रिप्टो-सुरक्षितप्रदर्शनउपयोग
kotlin.random.Randomहाँ (ThreadLocal)नहींतेज़सामान्य उद्देश्य
java.util.Randomनहीं (सिंक्रनाइज़्ड)नहींधीमालीगेसी कोड
ThreadLocalRandomहाँ (प्रति थ्रेड)नहींसबसे तेज़उच्च-प्रदर्शन बहु-थ्रेडेड
SecureRandomहाँहाँधीमासुरक्षा, टोकन, पासवर्ड

उन्नत: भारित यादृच्छिक चयन

fun  weightedRandom(items: List>): T {
    val totalWeight = items.sumOf { it.second }
    var random = Random.nextInt(totalWeight)
    
    for ((item, weight) in items) {
        if (random < weight) return item
        random -= weight
    }
    return items.last().first
}

// उपयोग: 70% मौका "सामान्य", 25% "दुर्लभ", 5% "महाकाव्य"
val rarity = weightedRandom(
    listOf("सामान्य" to 70, "दुर्लभ" to 25, "महाकाव्य" to 5)
)
  1. - JavaScript में एक विशिष्ट सीमा में यादृच्छिक पूर्ण संख्याएं कैसे उत्पन्न करें?

    यादृच्छिक संख्या उत्पन्न करने के लिए, JavaScript Math.random() विधि का उपयोग करें। यहाँ न्यूनतम और अधिकतम मान सेट करें और उनके बीच एक यादृच्छिक संख्या उत्पन्न करें जैसा कि निम्नलिखित कोड में दिखाया गया है: उदाहरण <!DOCTYPE html> <html> <body> <script>

  1. Android SQLite में random() फ़ंक्शन का उपयोग कैसे करें? Android SQLite में random() फ़ंक्शन का उपयोग कैसे करें?

    Android SQLite में RANDOM() फ़ंक्शन SQLite Android में एक ओपन-सोर्स SQL डेटाबेस है जो डेटा को डिवाइस पर टेक्स्ट फ़ाइल के रूप में स्टोर करता है। Android में SQLite का बिल्ट-इन कार्यान्वयन होता है। यह उदाहरण दर्शाता है कि Android SQLite में RANDOM() फ़ंक्शन का उपयोग करके रिकॉर्ड को रैंडम ऑर्डर में कैस

  1. एंड्रॉइड में दी गई रेंज में रैंडम नंबर कैसे जनरेट करें? एंड्रॉइड में दी गई रेंज में रैंडम नंबर कैसे जनरेट करें?

    एंड्रॉइड में रेंज-बाउंड रैंडम नंबर जनरेशन यह ट्यूटोरियल दर्शाता है कि उपयोगकर्ता-निर्दिष्ट न्यूनतम और अधिकतम मानों के बीच रैंडम इंटीजर कैसे जनरेट करें, इनपुट वैलिडेशन और एरर हैंडलिंग के साथ। लेआउट (activity_main.xml) <LinearLayout xmlns:android=https://schemas.android.com/apk/res/android and