dd if=/dev/urandom of=a.log bs=1M count=2Question: if this code ^ gives me random numbers, how can I get random non-words (unintelligible) such as “hgcqw”.
Question 2: Can the shuf command be used in conjunction with the dd command?
Thanks!
6/21 Update. (Father's day)
@ Serg My goal was to obtain numbers at first. Say, random two digits numbers from 01-100. Give the command and run the code. Eventually it will give me 14, 17, 21, 35 ( I am making up what random numbers I get). Next, I wanted to get an average of those numbers. In this case the average would be 21.75 (22). The goal would be to stop the loop until it finds a specific average I was looking for. In this case it would be 25. My second goal was to get random words and then get a count of what words were used.
@ terdon
A word is "buck", "duck," or "good old luck." Basically anything found in the dictionary. The lengths would be any yet if we were to constrain the limit to 5 letters, I wouldn't mind learning how?
What's a word? I presume you mean only a-z and A-Z but what lengths? Would liwyduglashvdbalishfvgapivflakhf be a word? How about ksjdas-asd-asd-asd? - can be in words. Please edit and clarify. – terdon yesterday
46 Answers
Generate a random alphabetic character.
tr -cd '[:alpha:]' < /dev/urandom | fold -w1 | head -n1 Generate a random alphabetic string 10 characters in length.
tr -cd '[:alpha:]' < /dev/urandom | fold -w10 | head -n1Some of the alternative POSIX character classes to use instead of the alpha in [:alpha:] are:alnum - alphanumeric characters
alpha - alphabetic characters
digit - digits
graph - visible characters
lower - lowercase characters
upper - uppercase characters
Generate random numbers in a specific range (example 1-10).
shuf -i 1-10 -n1 Compared to the first two bash examples, the shuf command offers fewer options for generating random strings , although shuf can be used to generate random permutations.
dd if=/dev/urandom of=a.log bs=1M count=2
This doesn't give you random numbers. This gives you random bytes. You could encode it to numbers:
hexdump -v -e '"%d\n"' /dev/urandom | dd of=a.log bs=1M count=2(The earlier version used od, but od's output format is rather inflexible.)
Using /dev/random directly might be rather wasteful, since it is a stream of bytes. You could encode it to base64, and then strip out non-alphabets, which should still get you, on an average, 80 % of the input data:
base64 /dev/urandom | tr -cd '[[:alpha:][:space:]]'This tends to leave rather long lines. You could, instead, replace numbers with newlines:
base64 /dev/urandom | sed 's/[^[:alpha:]]\+/\n/g' 9 To generate a random alphabetic lower-case string (including spaces) of 2MB:
< /dev/urandom tr -dc 'a-z ' | head -c 2000000 > a.logFor a constant stream:
< /dev/urandom tr -dc 'a-z 'To generate a random alphabetic mixed-case string (including spaces) of 2MB:
< /dev/urandom tr -dc 'A-Za-z ' | head -c 2000000 > a.logFor a constant stream:
< /dev/urandom tr -dc 'A-Za-z ' 4 An other version (-c${1:-10} = length of 10):
< /dev/urandom tr -dc A-Za-z | head -c${1:-10} > outor (head -n 10 = length of 10)
strings /dev/urandom | grep -o '[[:alpha:]]' | head -n 10 | tr -d '\n' > outor with dd (count=10 = length of 10)
dd if=/dev/urandom bs=1 count=10 2>/dev/null | base64 | grep -o '[[:alpha:]]' | head -n 30 | tr -d '\n' > outor this
openssl rand -base64 32| grep -o '[[:alpha:]]' | head -n 10 | tr -d '\n' > outor this
date +%s | sha256sum | base64 | grep -o '[[:alpha:]]'| head -n 10 | tr -d '\n' > out Here's my idea. mkpasswd is used often to generate encrypted passwords, hence the strings there shouldn't be intelligible already. The small problem is that it generates strings with numbers. By using sed, we can get rid of those.
mkpasswd | sed 's/[0-9]/YOLO/g'
YOLO could be whatever you want to substitute digits. You could also just delete them:
mkpasswd | sed 's/[0-9]//g'
Here's another way:
$ date | md5pass | sed 's/[0-9]//g'
$$NG.dLw$vcpGCylnDtmptDtogDr/As muru suggested in the comments bellow , you can also install pwgen, with sudo apt-get install pwgen, and run pwgen -0. The -0 flag tells the command to avoid numerals
And here's sha1pass + sed combo:
sha1pass | sed 's/[[:digit:]]//g' | sed 's/[[:punct:]]//g'
Result: PoXGAlAWjzMHgmfzHiYHGpemzqE
Another method with $RANDOM variable: echo $RANDOM | tr '0-9' 'a-z'
Overall, if your goal is password generation, I suggest you read this article:
4Using python:
#!/usr/bin/env python2
import random, string
def rand_gen(number_of_words, length): for i in range(number_of_words): print ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase) for _ in range(length)) returnWe have defined a function
rand_gen(number_of_words, length)that will take two parameters as inputs,number_of_wordswill contain the number of random words you want to generate,lengthwill contain the length of each random wordstring.ascii_lowercaseandstring.ascii_uppercasehave all the ASCII lowercase and uppercse letters respectivelyrandom.choicewill choose a single character from[A-Za-z]each time andlengthwill ensure that this iteration continues upto thelengthnumber.join()will then join the characters to have a word of desired lengthThis process will be continued till
number_of_words
Example :
rand_gen(5, 3)
PDI
qKQ
Dvu
kwr
wDz
rand_gen(2, 5)
GLKXr
uOaHj