0

I have a piece of code which generates a diceware-ready list of rolls but doesn't seem ideal.

strings -n 1 rd | egrep -o "[1-6]" | tr -d "\n" | fold -w5 > dice

Since it only looks for [1-6] it takes a lot more data than it needs to to generate a list. The output looks like this:

15531
52142
13645
62143
66211
11255
11124
21166
66555
66632
11111

In an attempt to alleviate this I found the following:

echo $((0x$(head -c5 rd | xxd -ps)%6+1))

However, I have not been able to modify it to work how I want it to. As expected this only outputs 1 random dice roll. As an example it would output:

3

It doesn't go any further into the file. I'd like it to work through the entire file (like the first piece of code) and output diceware-ready lines of numbers.

Ultimately I'd like to have the program automatically replace the rolled pairs with their corresponding diceware rolls. Using the above rolls the output would change to this:

ajar
rookie
benny
uh
47th
acrid
aback
coca
8:30
96
a
Ziggy
  • 1
  • 3
  • Consider showing us examples of the output you want and what you currently get. As is, I can only tell you want to do something with random numbers. – Jeffrey Bosboom Mar 09 '15 at 02:27
  • I've updated the question with some examples of both what I am currently getting and what I am hoping to get. – Ziggy Mar 09 '15 at 02:45
  • Why generate the intermediate file? It's easy to generate a list of random numbers. – Schwern Mar 09 '15 at 02:51
  • @Schwern Mainly for fun. I was trying to use something similar to Random.org that could be generated offline. – Ziggy Mar 09 '15 at 03:11

1 Answers1

3

Bash offers $RANDOM to generate a random number from 0 to 32767.

RANDOM Each time this parameter is referenced, a random integer between 0 and 32767 is generated. The sequence of random numbers may be initialized by assigning a value to RANDOM. If RANDOM is unset, it loses its special properties, even if it is subsequently reset.

It's not a great amount of randomness, but should be enough for your password generator. The answers over here explain how to get a specific random range.

Alternatively, Perl can do what you want quite well. Here it is as a one liner...

$ perl -nwle 'push @words, $_;  END { print join ".", map { $words[rand @words] } 1..3 }' /usr/share/dict/words 

Or more readably as a small program.

#!/usr/bin/env perl

use strict;
use warnings;
use autodie;  # IO functions will error if they fail

# Read the dictionary
open my $fh, "/usr/share/dict/words";
my @words = <$fh>;
chomp @words;

# Pick three random words joined with a .
print join ".", map { $words[rand @words] } 1..3;
Schwern
  • 153,029
  • 25
  • 195
  • 336