 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to generate large random numbers in Java?
For large random numbers, use BigInteger type in Java. At first, create a Random object −
Random randNum = new Random();
Now, declare a byte array and generate random bytes −
byte[] b = new byte[max]; randNum.nextBytes(b);
Now, generate a large random number with BigInteger type −
BigInteger bigInt = new BigInteger(b);
Example
import java.math.BigInteger; import java.util.Random; public class Demo {    public static void main(String... a) {       int max = 10;       Random randNum = new Random();       byte[] b = new byte[max];       randNum.nextBytes(b);       // BigInteger type       BigInteger bigInt = new BigInteger(b);       System.out.println("Very large random number = "+bigInt);    } }  Output
Very large random number = 283258678185163472552410
Advertisements
 