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.
I am playing with a script to generate random characters--ie UC or LC letters, numbers, etc.
After generating a random number (decimal), I use this statement to generate the character:
Code:
printf "\x$(printf "%x" $a)"
("a" is the decimal # already generated.)
This works perfectly--my question is" Is there a simpler way?
The best solution would be a direct way of generating random characters, but I have not seen such a thing.
you could set an array of characters, then set a random number x within the range of 0 - number of array elements-1 and take the charArr[x] as random character.
If want gibberish, then cat /dev/urandom or cat /dev/random will give you that (urandom being pseudo random, whereas random is more or less truly random, but is finite without external input - e.g. mouse movement etc.). Use strings to turn it into something a bit more recognisable:
Code:
strings /dev/urandom
or
strings /dev/random
edit: to test this, might be worth piping it into less, otherwise your terminal will very quickly fill up with rubbish!
The array method came to me while going to sleep--that one is the obvious head-slapper.
With the strings method, I don't see the easy way of limiting to a range---ie all LC letters. Also when I did "strings /dev/random" it printed maybe ten characters and then stopped.
No "pwgen" on this machine---will have to try that.
Also when I did "strings /dev/random" it printed maybe ten characters and then stopped.
If you do "strings /dev/random", and then wiggle the mouse for a few seconds, it will create new random characters. man urandom has this to say on it:
Quote:
Originally Posted by man urandom
The /dev/random interface returns random bytes
only when sufficient amount of entropy has been collected.
If there is no entropy to produce the requested number of
bytes, /dev/random blocks until more entropy can be
obtained.
...
Bytes retrieved from /dev/random provide the highest quality
random numbers produced by the generator, and can be used to
generate long term keys and other high value keying
material.
If you only want lowercase/uppercase/whatevercase, perhaps a pipe to tr is your friend?
If you do "strings /dev/random", and then wiggle the mouse for a few seconds, it will create new random characters. man urandom has this to say on it:If you only want lowercase/uppercase/whatevercase, perhaps a pipe to tr is your friend?
Strings seems to be a really slow way of getting random letters, numerals, etc.
For setting the range, I think throwing away certain characters would skew the statistics. To generate random numbers, I first generate from 0 to an upper limit by using modulo division. Then I add a constant to set the lower end. This--in principle--gives a gaussian centered in the middle of the final range. I don't think you would get a gaussian distribution by just discarding.
This is really turning into an exercise in programming techniques---there is actually not a big market for random letters....
In post #1, where were you getting the decimal random #?
If you adopted the array method in post #2, how would you map the random # onto the array index? And I mean specific bash code -- i.e. will built-in bash arithmetic suffice, or will you need bc or something like it?
BTW, I agree:
Quote:
throwing away certain characters would skew the statistics
I have not used the array method, but I assume that you would simply do:
num=$RANDOM
char=array[$num]
To avoid just throwing things away that don't fit the range (LO to HI), I set an upper bound which is the difference between HI and LO, then add LO back to the random number
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.