Java Program to create random BigInteger within a given range

Java 8Object Oriented ProgrammingProgramming

Create a Random class object −

Random random = new Random();

Now let us create a random BigInteger within the range set below −

BigInteger i = new BigInteger(1024, random);

Example

 Live Demo

import java.math.BigInteger;
import java.util.Random;
public class Demo {
   public static void main(String[] args) {
      Random random = new Random();
      BigInteger i = new BigInteger(1024, random);
      System.out.println("Random BigInteger = "+i);
   }
}

Output

Random BigInteger = 172988250696765715389833755481951104504569480256142363412177847577736195982554760981595486734850686498607899498518922990162874103419942352546847073039976287403845518172884710426458735414702299598858093908453406020426533304237452347610248118892069951238970445771615614781904759399589093691142208659284351662649
raja
Published on 25-Apr-2019 09:55:43
Advertisements