How do I generate a random n digit integer in Java using the BigInteger class?

makaveli2178 :

I am unsure about how to generate a random n digit integer in Java using the BigInteger class.

Eyal Schneider :
private static Random rnd = new Random();

public static String getRandomNumber(int digCount) {
    StringBuilder sb = new StringBuilder(digCount);
    for(int i=0; i < digCount; i++)
        sb.append((char)('0' + rnd.nextInt(10)));
    return sb.toString();
}

And then you can use it:

new BigInteger(getRandomNumber(10000))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

generate 4 digit random number using substring

How to generate a random BigInteger value in Java?

How do I generate a random integer between min and max in Java?

How to generate a random five digit number Java

how can I get nth digit in a BigInteger?

How do I generate random numbers in Dart?

How do I generate a random int number?

How do I generate random doubles in an Array?

How do I generate a random num::BigUint?

How do I generate a random derangement of a list/array in java?

DART: How do I generate a random six (6) digit number in flutter on a button click to display on the next page?

How do I generate random positions of hexagon using svg?

How to generate 14 digit long random number in postman using lodash?

How do i find the right most digit of a number integer with java without using number % 10? see the description

How can I sum the second digit to the last digit in an integer on java?

How do I generate a random number using functional state?

How to generate random single digit number using shell

How do I create a 2 digit random number with RNGCryptoServiceProvider class?

How do I generate any random date between 01/01/2016 to 01/01/2017 using Java?

How to generate 4 digit random numbers in java from 0000 to 9999

How do I generate a primary key that is not random?

How do i generate a random numbers for each integer in an array?

How do i generate random 1 digit numbers and then add them in each cell of a datagridview in c# window form application?

How do I generate multiple random variables in Java?

How to generate unto n digit combinations of an integer?

How do I generate a seven digit random number which is itself divisible by seven

how do I generate random group names using lists

How do I generate a random integer in C#?

How to generate 15 digit random number using Scala

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive