Java की Random Class
In this page:
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
Seeded Randomness
एक Random instance को explicitly seed करना (new Random(42)) इसके output sequence को deterministic और repeatable बनाता है, जो debugging या consistent random values चाहिए वाले tests लिखने के लिए valuable है।
उदाहरण: Seeded Randomness
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));
}
}
Login to try C/C++/Java/PHP code in the editor
ThreadLocalRandom
ThreadLocalRandom.current() हर thread को अपना independent random generator देता है, उस contention से बचते हुए जो तब होता है जब कई threads heavy concurrent use में एक single Random instance share करते हैं।
उदाहरण: ThreadLocalRandom
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Secure Randomness
SecureRandom tokens या keys generate करने जैसे security-sensitive uses के लिए suitable cryptographically strong randomness produce करता है — regular Random इतना predictable है कि उस purpose के लिए कभी उपयोग नहीं किया जाना चाहिए।
उदाहरण: Secure Randomness
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);
}
}
Login to try C/C++/Java/PHP code in the editor
- हर call पर एक नया
Randomobject बनाना, जो repeated values दे सकता है। - यह उम्मीद करना कि
nextInt(10)में 10 शामिल होगा, जब range 0 से 9 है। - Security purposes के लिए
Randomउपयोग करना, जबSecureRandomचाहिए।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: