Accidentally Discovering a Password Generator in Perl 6

Would you like a random number?

say rand;
# 0.468924762180761

How about between 0-10?

say 10.rand;
# 9.18202878042811

Let’s cut that down to an integer

say 10.rand.Int;
# 7

Let’s create an array of things from a range

say ["c"..."m"];
# [c d e f g h i j k l m]

How about we pick one of those

say ["c".."m"].pick;
# f

Suppose we don’t want to fill an array with a big range. Suppose we just want to represent a range, and pick from it without having to allocate that whole array

say pick 5, "0".."z";
# (y e E < m)

Pick, once it’s picked something, will never pick it again. It’s picked, and it’s gone. But if you want to do more like rolling dice and get repeats, use roll instead of pick

say roll 5, "0".."z";
# (F c = o =)

See how the output is in parenthesis? That’s because it’s returning a sequence

say (roll 5, "0".."z").WHAT;
# (Seq)

You can iterate over those if you like, or do whatever. Or you can join them into a single string

say (roll 5, "0".."z").join;

That’s looking suspiciously familiar. How about we generate a random number of rolls

say (roll 10.rand.Int, "0".."z").join;
# _JJF

Oh dear that’s not very many. Let’s say we have to have at least 12!

say (roll 10.rand.Int + 12, "0".."z").join;
# pCKtVB4j=^ShIZHyI

How about we have 10 of those to choose from?

for 0..9 {
    say (roll 10.rand.Int + 12, "0".."z").join;
# tEApXfu<klX<yn;Z6NXD4
# =n?GLn1QG3JBD2r_vz
# X49tYWtSap`H
# gyS[7_5Y5?w^
# j>oD4A4iTixa_87wVq
# ltSn<rDEM7Z^
# eyU_d?49^1Z:7;I=?1l
# nIFo2ZF20>l5j>Ord9
# cfKrtqjt3XG<\1G2o=
# TeJnVgy2qyJRBlB

Or if you wanted to, maybe you’d prefer, up to 10 😉 (and some other clarity)

for ^10 {
    say (roll (10.rand.Int + 12), "0".."z").join;