How do I generate a random number between 2 values

Hello Team,

I have a coding question about how do I generate a random number whose minimum character length is 2 and max is 4. In other words a random number between 10 and 9999

I am aware that we can generate a particular length using RandomStringUtils.randomNumeric(4) but not able to figure out how do I generate a random number between 2 different values

Thanks,

1 Like

int randomInt = new Random().nextInt(9999 - 10 + 1) + 10

Thanks @Brandon_Hein This works for me.

Before your reply I was able to generate a random number between 10 and 9999 using excel and then calling that cell within the script because I was thinking no body I going to help me today. lol

More generally (for people reading this that want to use different bounds), the algorithm is:

new Random().nextInt(upperBound - lowerBound + 1) + lowerBound

1 Like

There’s a tip waiting to be written, Brandon…

Or, if you prefer…

“Your mission, should you choose to accept it…” :wink:

Plus random strings/words, fixed character set…

Maybe, although a lot of these are already topics in Stack Overflow. Maybe I will aggregate a bunch of these related topics into one tip in the Katalon context, like you mentioned.

2 Likes

You have the Java background, I don’t. It’ll certainly prove a worthy clearinghouse for me.

1 Like

Here ya go :sunglasses:

Thanks for the suggestion.

3 Likes