# How to get a random number within a range in command line?

I need to generate random phone numbers in some curl commands across this thread about getting random numbers in the command line and I'm wondering how I can do this with a certain number range. The solution there was just to use `\$((RANDOM % 4))` because the user needed to generate a random number between 1-3.

For phone numbers my random number would have to be between 1000000000 and 9999999999. How can I generate a random number with a upper and lower range?

Thanks for any help!

• Different nations have different numbering plans which make many numeric sequences invalid phone numbers which cannot be dialed (e.g., any number in North America where the area code is between 000 and 200 inclusive). See the North American Numbering Plan en.wikipedia.org/wiki/North_American_Numbering_Plan and en.wikipedia.org/wiki/… for one such example. If you want to autogenerate a list of possible phone numbers, you need to specify which nation this is for. – K7AAY Feb 26 at 19:07
• Thanks, but it doesn't have to strictly be a valid phone number, just some 9 digit number. This is for testing purposes. – intA Feb 26 at 19:18
• OK. 10 digit number? Your examples were 10 digits. – K7AAY Feb 26 at 19:19
• whoops, yes, 10 digit – intA Feb 26 at 20:11

``````shuf -i 1-100 -n 1 # for range of 1-100 inclusively, output one line
``````

or in the general case

``````shuf -i LO-HI -n 1
``````

Replace `LO` with an integer that is the lower limit of the range, and replace `HI` with an integer that is the upper limit.

To pick a random file from files named something like:

``````file0.jpg
file1.jpg
file2.jpg
file3.jpg
``````

Run the following command to output a random file from file0.jpg to file3.jpg:

``````echo "file\$(shuf -i 0-3 -n 1).jpg"
``````

If you want something that looks more like a phone number, you might want to look at perl's String::Random module, ex.

``````\$ perl -MString::Random=random_regex -E "say random_regex('\d{3}-\d{3}-\d{4}')"
694-959-9937
``````