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

Post History

60%
+1 −0
Q&A How do you generate random strings from /dev/random?

/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...

posted 8mo ago by matthewsnyder‭  ·  edited 8mo ago by matthewsnyder‭

Answer
#2: Post edited by user avatar matthewsnyder‭ · 2024-03-24T01:32:55Z (8 months ago)
  • `/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 obviously if the values you want are such that only 1% of what comes out of `/dev/random` is acceptable, then it will 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`".
  • `/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`".
#1: Initial revision by user avatar matthewsnyder‭ · 2024-03-24T00:38:16Z (8 months ago)
`/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 obviously if the values you want are such that only 1% of what comes out of `/dev/random` is acceptable, then it will 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`".