Generate an N-Digit Random Number

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

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))

Generate 6 digit random number

Its as simple as that, you can use your code and just do one thing extra here

String.format("%06d", number);

this will return your number in string format, so the "0" will be "000000".

Here is the code.

public static String getRandomNumberString() {
// It will generate 6 digit random Number.
// from 0 to 999999
Random rnd = new Random();
int number = rnd.nextInt(999999);

// this will convert any number sequence into 6 character.
return String.format("%06d", number);
}

Generate random number of n digits or more

You can use the constructor BigInteger(int numBits, Random rnd) to generate positive random numbers with N bits.

As you want to have a minimum, you can add that as an offset to the generated numbers:

Random random = ThreadLocalRandom.current();
BigInteger base = BigInteger.valueOf(10000000); // min
int randomBits = 50; // set as many bits as you fancy

BigInteger rnd = base.add(new BigInteger(randomBits, random));

How to Generate a random number of fixed length using JavaScript?

console.log(Math.floor(100000 + Math.random() * 900000));

How to generate a random number with a specific amount of digits?

You can use either of random.randint or random.randrange. So to get a random 3-digit number:

from random import randint, randrange

randint(100, 999) # randint is inclusive at both ends
randrange(100, 1000) # randrange is exclusive at the stop

* Assuming you really meant three digits, rather than "up to three digits".


To use an arbitrary number of digits:

from random import randint

def random_with_N_digits(n):
range_start = 10**(n-1)
range_end = (10**n)-1
return randint(range_start, range_end)

print random_with_N_digits(2)
print random_with_N_digits(3)
print random_with_N_digits(4)

Output:

33
124
5127

Generating random number of N-digits using gmpy2 library in Python

You should specify the minimum, and maximum number of random numbers given N as follows:

Code:

# conda install -c conda-forge gmpy2

def get_ranged_random_integer(rand_min, rand_max):
import gmpy2
rs = gmpy2.random_state(hash(gmpy2.random_state()))
return rand_min + gmpy2.mpz_random(rs, rand_max - rand_min + 1)

def get_random_intger_of_N_digits(n):
rand_min = 10**(n-1)
rand_max = (10**n)-1
return get_ranged_random_integer(rand_min, rand_max)

if __name__ == '__main__':
n = 100
p = get_random_intger_of_N_digits(n)
print(f"[main] length: {len(str(p))}, random: {p}")

Result:

[main] length: 100, random: 2822384188052405746651605684545963323038180536388629939634656717599213762102793104021248192535427134

Generate a random integer with a specified number of digits Java

private long generateRandomNumber(int n) {
long min = (long) Math.pow(10, n - 1);
return ThreadLocalRandom.current().nextLong(min, min * 10);
}

nextLong produces random numbers between lower bound inclusive and upper bound exclusive so calling it with parameters (1_000, 10_000) for example results in numbers 1000 to 9999.
Old Random did not get those nice new features unfortunately. But there is basically no reason to continue to use it anyways.



Related Topics



Leave a reply



Submit