← Back to Core Java Course | Chapter 13: Advanced Topics & Reference | Lesson 3 of 10

Java की Random Class

Random class आपके program को chance choices बनाने देती है, dice roll करने जैसा। आप एक range में numbers भी चुन सकते हैं या वही pretend-random sequence दोहरा सकते हैं।
Syntax
java
import java.util.Random;

Random random = new Random();
random.nextInt(bound);

The Random Class

Random एक seed value से pseudo-random numbers generate करता है — एक जैसे seed के साथ बनाए दो Random instances वही exact sequence of values produce करेंगे, जो reproducible tests के लिए उपयोगी है।

उदाहरण: The Random Class

java
import java.util.Random;
public class Main {
	public static void main(String[] args) {
		Random r1 = new Random(42);
		Random r2 = new Random(42); // same seed
		System.out.println(r1.nextInt(100) == r2.nextInt(100)); // true
	}
}

Random in a Range

nextInt(bound) output को zero से शुरू होने वाली एक range तक restrict करता है; 10-20 जैसी एक arbitrary range में एक value generate करने के लिए, आप आमतौर पर min + random.nextInt(max - min + 1) compute करते हैं।

उदाहरण: Random in a Range

java
// Import java.util.Random so it can be used by its short name
import java.util.Random;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Random object and store it in random
		Random random = new Random(1);
		int min = 10, max = 20;
		int value = min + random.nextInt(max - min + 1);
		// Print a line to the console
		System.out.println(value >= 10 && value <= 20);
	}
}

Seeded Randomness

एक Random instance को explicitly seed करना (new Random(42)) इसके output sequence को deterministic और repeatable बनाता है, जो debugging या consistent random values चाहिए वाले tests लिखने के लिए valuable है।

उदाहरण: Seeded Randomness

java
import java.util.Random;
public class Main {
	public static void main(String[] args) {
		Random random = new Random(42); // deterministic sequence
		System.out.println(random.nextInt(100));
		System.out.println(random.nextInt(100));
	}
}

ThreadLocalRandom

ThreadLocalRandom.current() हर thread को अपना independent random generator देता है, उस contention से बचते हुए जो तब होता है जब कई threads heavy concurrent use में एक single Random instance share करते हैं।

उदाहरण: ThreadLocalRandom

java
import java.util.concurrent.ThreadLocalRandom;
public class Main {
	public static void main(String[] args) {
		int value = ThreadLocalRandom.current().nextInt(1, 10); // own generator per thread
		System.out.println(value >= 1 && value < 10);
	}
}

Secure Randomness

SecureRandom tokens या keys generate करने जैसे security-sensitive uses के लिए suitable cryptographically strong randomness produce करता है — regular Random इतना predictable है कि उस purpose के लिए कभी उपयोग नहीं किया जाना चाहिए।

उदाहरण: Secure Randomness

java
import java.security.SecureRandom;
public class Main {
	public static void main(String[] args) {
		SecureRandom secureRandom = new SecureRandom(); // cryptographically strong
		int token = secureRandom.nextInt(1000000);
		System.out.println(token >= 0);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. हर call पर एक नया Random object बनाना, जो repeated values दे सकता है।
  2. यह उम्मीद करना कि nextInt(10) में 10 शामिल होगा, जब range 0 से 9 है।
  3. Security purposes के लिए Random उपयोग करना, जब SecureRandom चाहिए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.