एंड्रॉइड में रेंज-बाउंड रैंडम नंबर जनरेशन
यह ट्यूटोरियल दर्शाता है कि उपयोगकर्ता-निर्दिष्ट न्यूनतम और अधिकतम मानों के बीच रैंडम इंटीजर कैसे जनरेट करें, इनपुट वैलिडेशन और एरर हैंडलिंग के साथ।
लेआउट (activity_main.xml)
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp"
android:gravity="center">
<EditText
android:id="@+id/et_min"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="न्यूनतम मान"
android:inputType="number"
android:layout_marginBottom="16dp" />
<EditText
android:id="@+id/et_max"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="अधिकतम मान"
android:inputType="number"
android:layout_marginBottom="24dp" />
<Button
android:id="@+id/btn_generate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="जनरेट करें"
android:layout_marginBottom="24dp" />
<TextView
android:id="@+id/tv_output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp"
android:textStyle="bold"
android:hint="परिणाम" />
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class MainActivity extends AppCompatActivity {
private EditText etMin, etMax;
private Button btnGenerate;
private TextView tvOutput;
private final Random random = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etMin = findViewById(R.id.et_min);
etMax = findViewById(R.id.et_max);
btnGenerate = findViewById(R.id.btn_generate);
tvOutput = findViewById(R.id.tv_output);
btnGenerate.setOnClickListener(v -> generateRandomNumber());
}
private void generateRandomNumber() {
String minStr = etMin.getText().toString().trim();
String maxStr = etMax.getText().toString().trim();
if (minStr.isEmpty() || maxStr.isEmpty()) {
Toast.makeText(this, "कृपया दोनों मान दर्ज करें", Toast.LENGTH_SHORT).show();
return;
}
try {
int min = Integer.parseInt(minStr);
int max = Integer.parseInt(maxStr);
if (max <= min) {
Toast.makeText(this, "अधिकतम मान न्यूनतम से बड़ा होना चाहिए", Toast.LENGTH_SHORT).show();
return;
}
// विधि 1: java.util.Random (थ्रेड-सेफ नहीं, लेकिन सरल)
int randomNum = random.nextInt((max - min) + 1) + min;
// विधि 2: ThreadLocalRandom (बेहतर परफॉरमेंस, थ्रेड-सेफ) - Java 7+
// int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
// विधि 3: SecureRandom (क्रिप्टोग्राफिकली सिक्योर)
// SecureRandom secureRandom = new SecureRandom();
// int randomNum = secureRandom.nextInt((max - min) + 1) + min;
tvOutput.setText(String.valueOf(randomNum));
} catch (NumberFormatException e) {
Toast.makeText(this, "अमान्य संख्या प्रारूप", Toast.LENGTH_SHORT).show();
}
}
}
रैंडम जनरेशन विधियों की तुलना
| क्लास | थ्रेड-सेफ | क्रिप्टो-सिक्योर | उपयोग का मामला |
|---|---|---|---|
Random |
नहीं | नहीं | सामान्य उद्देश्य, सिंगल-थ्रेड |
ThreadLocalRandom |
हाँ | नहीं | मल्टी-थ्रेडेड, हाई परफॉरमेंस |
SecureRandom |
हाँ | हाँ | पासवर्ड, टोकन, सुरक्षा-संवेदनशील |
फ्लोट/डबल रैंडम नंबर्स
// 0.0 से 1.0 के बीच डबल double randomDouble = random.nextDouble(); // रेंज में डबल (min से max) double randomInRange = min + (max - min) * random.nextDouble(); // गॉसियन (सामान्य वितरण) double gaussian = random.nextGaussian(); // माध्य 0, मानक विचलन 1
Kotlin संस्करण
val random = (min..max).random() // कोटलिन 1.3+ // या val random = ThreadLocalRandom.current().nextInt(min, max + 1)