Skip to content

Instantly share code, notes, and snippets.

@ldante86
Created December 14, 2017 18:39
Show Gist options
  • Save ldante86/8ea79ef1a3527b12b0b6d75e1815fd5a to your computer and use it in GitHub Desktop.
Save ldante86/8ea79ef1a3527b12b0b6d75e1815fd5a to your computer and use it in GitHub Desktop.
Perl functions for printing random strings
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
# Purpose: generate random strings
# Usage: random1(50);
# Default length is 8.
sub random1 {
my @chars = qw(a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9 ! @ $ % & * ( ) - _ = + < . > / ? ' " : ; [ ]
{ } \ | ~ `);
my $len = 8;
$len = $_[0] if $_[0];
for ( my $i = 0 ; $i < $len ; $i++ ) {
print $chars[ int rand(@chars) ];
}
print "\n";
}
sub random2 {
my $temp;
my $len = 8;
$len = $_[0] if $_[0];
for ( my $i = 0 ; $i < $len ; $i++ ) {
$temp = int rand(126);
if ( $temp < 33 ) {
$i--;
next;
}
print chr($temp);
}
print "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment