Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

How do you generate random strings from /dev/random?

+1
−0

Of course every language has some kind of random library... But can you generate custom random strings with just basic CLI tools?

For example, we have /dev/random which provides a stream of random values... But they're random bytes, so a lot of them are non-printable or special characters. If I want a random number of random alphanumeric string, how do I get that from /dev/random?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+1
−0

/dev/random is a stream of every possible value. You're supposed to filter it to take the ones you want. This is efficient, although if the values you want are such that only, say, 1% of what comes out of /dev/random is acceptable, then it will obviously take 100x longer to generate them. But luckily /dev/random is very fast.

First, the foreword: cat /dev/random (yes, useless cat, whatever) will produce a forever stream of random data. This isn't very useful, usually you want a limited stream, you do it with head:

$ cat /dev/random | head -c 20
NL(c%t6ޖ[\{I⏎

This is taking 20 random chars. head is nice in that, even though the input is infinite, head will shut it off once it has what it needs (well, your shell will, whatever). You'll note it's actually 12 chars - head is getting 20 bytes, then they get rendered by my shell, some end up displayed as multi-byte Unicode chars. This unicode business will stop mattering in a minute. The newline is just added, it's not coming from /dev/random.

We now need to throw out characters we don't want. This must be some incantation that will produce only "good" chars, so that head can collect 20 of them and exit. The OG way is tr:

$ cat /dev/random | tr -cd A-Za-z0-9 | head -c 20
yBJ1Uh7yatwdZmgxVK8C⏎

-c is --complement aka invert. -d is --delete. So we have "delete everything that is not A-Za-z0-9".

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »