ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This works fine if you want a number in the 0 - 32767 (16-bit integer) range.
I need a larger number in the -2147483648 to 2147483647 (32-bit integer) range.
Is there another function like $RANDOM that provides a random number in this larger range. I've been looking for a while now and just keep turning up $RANDOM.
Yeah, that totally doesn't answer my question. I did search before posting, and only found other hackish solutions like the one you referred me to. While that solution could possibly solve a similiar problem.
I am not looking for a loop that gives me any x number of 0-9 integers, of course I could do that.
My question is:
"Is there another function like $RANDOM that provides a random number in this larger range?"
-2147483648 to 2147483647 (32-bit integer)
There may not be, and the only solution may be to write your own, but I'd rather seek out built in first.
Thank you for the reply, I like the direction you suggested. I'm still going to look around a bit for a built in function, although at this point it's looking bleak.
For random numbers of arbitrary length I once created this bash function:
Code:
#!/bin/bash
function random()
{
od -d /dev/urandom |\
sed -e 's/^[0-9]* //' -e 's/ //g' |\
while read L ; do echo -n $L ; done |\
dd bs=1 count=${1:-10} 2>/dev/null
}
A=$(random) # random number of 10 digits (default)
B=$(random 5) # random number of 5 digits
C=$(random 5000) # almost unlimited number of digits works.
echo $A
echo $B
echo $C
Because the kernel's random generator is used the randomness is better than bash's internal generator (I assume).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.