Format CalcRoots, Password, and RandomNumber.
- Fix mixed line endings. - Convert tabs to spaces.master
parent
ecd0e6d58a
commit
f90bd04fef
|
@ -18,126 +18,126 @@ zci is_cached => 1;
|
|||
zci answer_type => 'root';
|
||||
|
||||
handle query => sub {
|
||||
# The 'root' trigger is very ambigous so this regex provides the specific triggers
|
||||
return unless m/^((?:.*square|.*cube(?:d|)|.*th|.*rd|.*nd|.*st|.*[0-9]+)) root(?: of|) (?!of)(.*)/i;
|
||||
|
||||
# Common phrases that won't be caught by str2nbr()
|
||||
my %function = (
|
||||
'square' => 2,
|
||||
'cubed' => 3,
|
||||
'cube' => 3,
|
||||
);
|
||||
|
||||
# Seperate the exponent and base courtesy of the above regex
|
||||
my $exp = $1;
|
||||
my $base = $2;
|
||||
|
||||
# Figure out what number the exponent is
|
||||
if ($exp =~ m/negative\s|minus\s|\A-/i) {
|
||||
$exp = $function{$'} ? $function{$'} * -1 : str2nbr($') * -1;
|
||||
}
|
||||
else {
|
||||
$exp = $function{$exp} ? $function{$exp} : str2nbr($exp);
|
||||
}
|
||||
# The 'root' trigger is very ambigous so this regex provides the specific triggers
|
||||
return unless m/^((?:.*square|.*cube(?:d|)|.*th|.*rd|.*nd|.*st|.*[0-9]+)) root(?: of|) (?!of)(.*)/i;
|
||||
|
||||
# Common phrases that won't be caught by str2nbr()
|
||||
my %function = (
|
||||
'square' => 2,
|
||||
'cubed' => 3,
|
||||
'cube' => 3,
|
||||
);
|
||||
|
||||
# Seperate the exponent and base courtesy of the above regex
|
||||
my $exp = $1;
|
||||
my $base = $2;
|
||||
|
||||
# Figure out what number the exponent is
|
||||
if ($exp =~ m/negative\s|minus\s|\A-/i) {
|
||||
$exp = $function{$'} ? $function{$'} * -1 : str2nbr($') * -1;
|
||||
}
|
||||
else {
|
||||
$exp = $function{$exp} ? $function{$exp} : str2nbr($exp);
|
||||
}
|
||||
|
||||
# You can't take a zeroeth root
|
||||
return if $exp == 0;
|
||||
# You can't take a zeroeth root
|
||||
return if $exp == 0;
|
||||
|
||||
|
||||
# There are separate cases here.
|
||||
# 1. Negative base and even exponent (imaginary numbers)
|
||||
# 2. Negative base and odd exponenet
|
||||
# 3. Positive base
|
||||
# There are separate cases here.
|
||||
# 1. Negative base and even exponent (imaginary numbers)
|
||||
# 2. Negative base and odd exponenet
|
||||
# 3. Positive base
|
||||
|
||||
if ($base =~ m/negative\s|minus\s|\A-/i && $exp % 2 == 0) {
|
||||
|
||||
# Figure out what number the base is
|
||||
$base = $';
|
||||
$base = str2nbr($base) if $base =~ /[^0-9]/;
|
||||
|
||||
# Solve using the absolute value of the base
|
||||
$base = abs($base);
|
||||
my $calc = $base ** (1/$exp);
|
||||
|
||||
# If the result is a whole number (n), the answer is n*i
|
||||
if (($calc - int($calc)) == 0) {
|
||||
return $calc . 'i', html => "<sup>$exp</sup>√-$base = $calc<em>i</em>";
|
||||
}
|
||||
if ($base =~ m/negative\s|minus\s|\A-/i && $exp % 2 == 0) {
|
||||
|
||||
# Figure out what number the base is
|
||||
$base = $';
|
||||
$base = str2nbr($base) if $base =~ /[^0-9]/;
|
||||
|
||||
# Solve using the absolute value of the base
|
||||
$base = abs($base);
|
||||
my $calc = $base ** (1/$exp);
|
||||
|
||||
# If the result is a whole number (n), the answer is n*i
|
||||
if (($calc - int($calc)) == 0) {
|
||||
return $calc . 'i', html => "<sup>$exp</sup>√-$base = $calc<em>i</em>";
|
||||
}
|
||||
|
||||
# Try and simplify the radical
|
||||
my $count = int($calc);
|
||||
# Try and simplify the radical
|
||||
my $count = int($calc);
|
||||
|
||||
while ($count > 1) {
|
||||
|
||||
# See if the current number raised to the given exponent is a factor of our base. If it is, the answer is n * i * exponent-root(the other factor)
|
||||
my $newBase = $base / ($count ** $exp);
|
||||
while ($count > 1) {
|
||||
|
||||
# See if the current number raised to the given exponent is a factor of our base. If it is, the answer is n * i * exponent-root(the other factor)
|
||||
my $newBase = $base / ($count ** $exp);
|
||||
|
||||
if ( ($newBase - int($newBase)) == 0) {
|
||||
return "The $exp-root of -$base is $count * i * the $exp-root of $newBase.", html=> "<sup>$exp</sup>√-$base = $count<em>i</em>⋅<sup>$exp</sup>√$newBase";
|
||||
}
|
||||
if ( ($newBase - int($newBase)) == 0) {
|
||||
return "The $exp-root of -$base is $count * i * the $exp-root of $newBase.", html=> "<sup>$exp</sup>√-$base = $count<em>i</em>⋅<sup>$exp</sup>√$newBase";
|
||||
}
|
||||
|
||||
$count--;
|
||||
}
|
||||
$count--;
|
||||
}
|
||||
|
||||
# Can't be solved or simplified via the above methods
|
||||
return "The $exp-root of -$base is i * the $exp-root of $base", html => "<sup>$exp</sup>√-$base = <em>i</em>⋅<sup>$exp</sup>√$base";
|
||||
}
|
||||
elsif ($base =~ m/negative\s|minus\s|\A-/i && $exp % 2 != 0) {
|
||||
|
||||
# Solve normally
|
||||
$base = $';
|
||||
$base = str2nbr($base) if $base =~ m/[^0-9]/;
|
||||
$base =~ s/[^0-9\.]//g;
|
||||
if ($base ne '') {
|
||||
|
||||
my $calc = $base ** (1/$exp) * -1;
|
||||
|
||||
# Try and simplify the radical
|
||||
my $count = int(abs($calc));
|
||||
|
||||
while ($count > 1) {
|
||||
|
||||
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
|
||||
my $newBase = $base / ($count ** $exp);
|
||||
# Can't be solved or simplified via the above methods
|
||||
return "The $exp-root of -$base is i * the $exp-root of $base", html => "<sup>$exp</sup>√-$base = <em>i</em>⋅<sup>$exp</sup>√$base";
|
||||
}
|
||||
elsif ($base =~ m/negative\s|minus\s|\A-/i && $exp % 2 != 0) {
|
||||
|
||||
# Solve normally
|
||||
$base = $';
|
||||
$base = str2nbr($base) if $base =~ m/[^0-9]/;
|
||||
$base =~ s/[^0-9\.]//g;
|
||||
if ($base ne '') {
|
||||
|
||||
my $calc = $base ** (1/$exp) * -1;
|
||||
|
||||
# Try and simplify the radical
|
||||
my $count = int(abs($calc));
|
||||
|
||||
while ($count > 1) {
|
||||
|
||||
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
|
||||
my $newBase = $base / ($count ** $exp);
|
||||
|
||||
if ( ($newBase - int($newBase)) == 0) {
|
||||
return "The $exp-root of -$base is $calc (-$count times the $exp-root of $newBase).", html=> qq|<sup>$exp</sup>√-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> (-$count⋅<sup>$exp</sup>√$newBase)|;
|
||||
}
|
||||
if ( ($newBase - int($newBase)) == 0) {
|
||||
return "The $exp-root of -$base is $calc (-$count times the $exp-root of $newBase).", html=> qq|<sup>$exp</sup>√-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> (-$count⋅<sup>$exp</sup>√$newBase)|;
|
||||
}
|
||||
|
||||
$count--;
|
||||
}
|
||||
$count--;
|
||||
}
|
||||
|
||||
return "The $exp-root of -$base is $calc.", html=> qq|<sup>$exp</sup>√-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a>|;
|
||||
}
|
||||
}
|
||||
elsif ($exp =~ m/[0-9]+/) {
|
||||
|
||||
# Solve normally
|
||||
$base = str2nbr($base) if $base =~ m/[^0-9]/;
|
||||
$base =~ s/[^0-9\.]//g;
|
||||
if ($base ne '') {
|
||||
my $calc = $base ** (1/$exp);
|
||||
|
||||
# Try and simplify the radical
|
||||
my $count = int($calc);
|
||||
|
||||
while ($count > 1) {
|
||||
|
||||
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
|
||||
my $newBase = $base / ($count ** $exp);
|
||||
return "The $exp-root of -$base is $calc.", html=> qq|<sup>$exp</sup>√-$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a>|;
|
||||
}
|
||||
}
|
||||
elsif ($exp =~ m/[0-9]+/) {
|
||||
|
||||
# Solve normally
|
||||
$base = str2nbr($base) if $base =~ m/[^0-9]/;
|
||||
$base =~ s/[^0-9\.]//g;
|
||||
if ($base ne '') {
|
||||
my $calc = $base ** (1/$exp);
|
||||
|
||||
# Try and simplify the radical
|
||||
my $count = int($calc);
|
||||
|
||||
while ($count > 1) {
|
||||
|
||||
# See if the current number raised to the given exponent is a factor of our base. If it is, we can give them a simplified version of the radical in addition to the answer.
|
||||
my $newBase = $base / ($count ** $exp);
|
||||
|
||||
if ( ($newBase - int($newBase)) == 0) {
|
||||
return "The $exp-root of $base is $calc ($count times the $exp-root of $newBase).", html=> qq|<sup>$exp</sup>√$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> ($count⋅<sup>$exp</sup>√$newBase)|;
|
||||
}
|
||||
if ( ($newBase - int($newBase)) == 0) {
|
||||
return "The $exp-root of $base is $calc ($count times the $exp-root of $newBase).", html=> qq|<sup>$exp</sup>√$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a> ($count⋅<sup>$exp</sup>√$newBase)|;
|
||||
}
|
||||
|
||||
$count--;
|
||||
}
|
||||
$count--;
|
||||
}
|
||||
|
||||
return "The $exp-root of $base is $calc.", html => qq|<sup>$exp</sup>√$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a>|;
|
||||
}
|
||||
}
|
||||
return "The $exp-root of $base is $calc.", html => qq|<sup>$exp</sup>√$base = <a href="javascript:;" onclick="document.x.q.value='$calc';document.x.q.focus();">$calc</a>|;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
return;
|
||||
};
|
||||
|
||||
1;
|
||||
|
|
|
@ -1,121 +1,122 @@
|
|||
package DDG::Goodie::Password;
|
||||
|
||||
use DDG::Goodie;
|
||||
|
||||
primary_example_queries 'random password', 'random password strong 15';
|
||||
description 'generates a random password';
|
||||
name 'Password';
|
||||
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodies/Password.pm';
|
||||
attribution github => ['https://github.com/duckduckgo', 'duckduckgo'];
|
||||
category 'computing_tools';
|
||||
topics 'cryptography';
|
||||
|
||||
zci answer_type => 'pw';
|
||||
|
||||
triggers any => 'random', 'password', 'pw', 'pwgen';
|
||||
|
||||
# exclude lower case l's, upper case I's and O's.
|
||||
my @pwgen_avg = ('a'..'k','m'..'z','A'..'H','J'..'N','P'..'Z',0..9);
|
||||
my @pwgen_weak = ('a'..'k','m'..'z','A'..'H','J'..'N','P'..'Z');
|
||||
my @pwgen_strong = ('a'..'k','m'..'z','A'..'H','J'..'N','P'..'Z',0..9,'!','@','#','$','%','^','&','*','(',')');
|
||||
|
||||
my %pw_strengths = (
|
||||
'strong' => 'strong',
|
||||
'hard' => 'strong',
|
||||
'weak' => 'weak',
|
||||
'average' => 'avg',
|
||||
'avg' => 'avg',
|
||||
);
|
||||
|
||||
handle query_lc => sub {
|
||||
if ( $_ =~ /^\!?(?:(?:random |)password(?: generator|)|pw(?:gen|))(?: |)(\d+|strong|hard|weak|average|avg|)(?: |)(\d+|strong|hard|weak|average|avg|)$/i ) {
|
||||
|
||||
# Actually make it random.
|
||||
srand();
|
||||
|
||||
|
||||
my $var1 = $1 || '';
|
||||
my $var2 = $2 || '';
|
||||
|
||||
# For debugging.
|
||||
# warn qq(VARS: $var1\t$var2\n);
|
||||
|
||||
my $pw_length = 8;
|
||||
$pw_length = $var1 if $var1 && $var1 =~ /^\d+$/;
|
||||
$pw_length = $var2 if $var2 && $var2 =~ /^\d+$/;
|
||||
$pw_length = 1 if $pw_length < 1;
|
||||
$pw_length = 32 if $pw_length > 32;
|
||||
|
||||
# For debugging.
|
||||
# warn $pw_length;
|
||||
|
||||
my $pw_strength = 'avg';
|
||||
$pw_strength = $pw_strengths{ lc $var1 } if $var1 && exists $pw_strengths{ lc $var1 };
|
||||
$pw_strength = $pw_strengths{ lc $var2 } if $var2 && exists $pw_strengths{ lc $var2 };
|
||||
|
||||
# For debugging.
|
||||
# warn $pw_strength;
|
||||
# warn exists $data->{pw_strengths}->{lc $var1};
|
||||
|
||||
# Password.
|
||||
my $pwgen = '';
|
||||
|
||||
# Generate random password.
|
||||
for ( my $i = 0 ; $i < $pw_length ; $i++ ) {
|
||||
my $rand = rand;
|
||||
|
||||
if ( $pw_strength eq 'weak' ) {
|
||||
$rand *= scalar(@pwgen_weak);
|
||||
$rand = int($rand);
|
||||
$pwgen .= $pwgen_weak[$rand];
|
||||
} elsif ( $pw_strength eq 'strong' ) {
|
||||
$rand *= scalar(@pwgen_strong);
|
||||
$rand = int($rand);
|
||||
$pwgen .= $pwgen_strong[$rand];
|
||||
} else {
|
||||
$rand *= scalar(@pwgen_avg);
|
||||
$rand = int($rand);
|
||||
$pwgen .= $pwgen_avg[$rand];
|
||||
}
|
||||
}
|
||||
|
||||
# Include at least one #.
|
||||
if ( ( $pw_strength eq 'avg' || $pw_strength eq 'strong' ) && $pwgen !~ /\d/ ) {
|
||||
my $rand = rand;
|
||||
$rand *= $pw_length - 1;
|
||||
$rand = int($rand) + 1;
|
||||
|
||||
my $rand2 = rand;
|
||||
$rand2 *= 9;
|
||||
$rand2 = int($rand2);
|
||||
|
||||
# Splice in number.
|
||||
$pwgen = substr( $pwgen, 0, $rand - 1 ) . $rand2 . substr( $pwgen, $rand );
|
||||
}
|
||||
|
||||
# Include at least one special char.
|
||||
if ( $pw_strength eq 'strong' && $pwgen !~ /[\!\@\#\$\%\^\&\*\(\)]/ ) {
|
||||
my $rand = rand;
|
||||
$rand *= $pw_length - 1;
|
||||
$rand = int($rand) + 1;
|
||||
|
||||
my $rand2 = rand;
|
||||
$rand2 *= 10;
|
||||
$rand2 = int($rand2);
|
||||
|
||||
my @rand2 = ( '!', '@', '#', '$', '%', '^', '&', '*', '(', ')' );
|
||||
|
||||
# For debugging.
|
||||
# warn "SPLICE $rand2", $rand2[$rand2], "\n";
|
||||
|
||||
# Splice in number.
|
||||
$pwgen = substr( $pwgen, 0, $rand - 1 ) . $rand2[$rand2] . substr( $pwgen, $rand );
|
||||
}
|
||||
|
||||
# Add password for display.
|
||||
return $pwgen." (random password)";
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
1;
|
||||
package DDG::Goodie::Password;
|
||||
|
||||
use DDG::Goodie;
|
||||
|
||||
primary_example_queries 'random password', 'random password strong 15';
|
||||
description 'generates a random password';
|
||||
name 'Password';
|
||||
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodies/Password.pm';
|
||||
attribution github => ['https://github.com/duckduckgo', 'duckduckgo'];
|
||||
category 'computing_tools';
|
||||
topics 'cryptography';
|
||||
|
||||
zci answer_type => 'pw';
|
||||
|
||||
triggers any => 'random', 'password', 'pw', 'pwgen';
|
||||
|
||||
# exclude lower case l's, upper case I's and O's.
|
||||
my @pwgen_avg = ('a'..'k','m'..'z','A'..'H','J'..'N','P'..'Z',0..9);
|
||||
my @pwgen_weak = ('a'..'k','m'..'z','A'..'H','J'..'N','P'..'Z');
|
||||
my @pwgen_strong = ('a'..'k','m'..'z','A'..'H','J'..'N','P'..'Z',0..9,'!','@','#','$','%','^','&','*','(',')');
|
||||
|
||||
my %pw_strengths = (
|
||||
'strong' => 'strong',
|
||||
'hard' => 'strong',
|
||||
'weak' => 'weak',
|
||||
'average' => 'avg',
|
||||
'avg' => 'avg',
|
||||
);
|
||||
|
||||
handle query_lc => sub {
|
||||
if ( $_ =~ /^\!?(?:(?:random |)password(?: generator|)|pw(?:gen|))(?: |)(\d+|strong|hard|weak|average|avg|)(?: |)(\d+|strong|hard|weak|average|avg|)$/i ) {
|
||||
|
||||
# Actually make it random.
|
||||
srand();
|
||||
|
||||
|
||||
my $var1 = $1 || '';
|
||||
my $var2 = $2 || '';
|
||||
|
||||
# For debugging.
|
||||
# warn qq(VARS: $var1\t$var2\n);
|
||||
|
||||
my $pw_length = 8;
|
||||
$pw_length = $var1 if $var1 && $var1 =~ /^\d+$/;
|
||||
$pw_length = $var2 if $var2 && $var2 =~ /^\d+$/;
|
||||
$pw_length = 1 if $pw_length < 1;
|
||||
$pw_length = 32 if $pw_length > 32;
|
||||
|
||||
# For debugging.
|
||||
# warn $pw_length;
|
||||
|
||||
my $pw_strength = 'avg';
|
||||
$pw_strength = $pw_strengths{ lc $var1 } if $var1 && exists $pw_strengths{ lc $var1 };
|
||||
$pw_strength = $pw_strengths{ lc $var2 } if $var2 && exists $pw_strengths{ lc $var2 };
|
||||
|
||||
# For debugging.
|
||||
# warn $pw_strength;
|
||||
# warn exists $data->{pw_strengths}->{lc $var1};
|
||||
|
||||
# Password.
|
||||
my $pwgen = '';
|
||||
|
||||
# Generate random password.
|
||||
for ( my $i = 0 ; $i < $pw_length ; $i++ ) {
|
||||
my $rand = rand;
|
||||
|
||||
if ( $pw_strength eq 'weak' ) {
|
||||
$rand *= scalar(@pwgen_weak);
|
||||
$rand = int($rand);
|
||||
$pwgen .= $pwgen_weak[$rand];
|
||||
} elsif ( $pw_strength eq 'strong' ) {
|
||||
$rand *= scalar(@pwgen_strong);
|
||||
$rand = int($rand);
|
||||
$pwgen .= $pwgen_strong[$rand];
|
||||
} else {
|
||||
$rand *= scalar(@pwgen_avg);
|
||||
$rand = int($rand);
|
||||
$pwgen .= $pwgen_avg[$rand];
|
||||
}
|
||||
}
|
||||
|
||||
# Include at least one #.
|
||||
if ( ( $pw_strength eq 'avg' || $pw_strength eq 'strong' ) && $pwgen !~ /\d/ ) {
|
||||
my $rand = rand;
|
||||
$rand *= $pw_length - 1;
|
||||
$rand = int($rand) + 1;
|
||||
|
||||
my $rand2 = rand;
|
||||
$rand2 *= 9;
|
||||
$rand2 = int($rand2);
|
||||
|
||||
# Splice in number.
|
||||
$pwgen = substr( $pwgen, 0, $rand - 1 ) . $rand2 . substr( $pwgen, $rand );
|
||||
}
|
||||
|
||||
# Include at least one special char.
|
||||
if ( $pw_strength eq 'strong' && $pwgen !~ /[\!\@\#\$\%\^\&\*\(\)]/ ) {
|
||||
my $rand = rand;
|
||||
$rand *= $pw_length - 1;
|
||||
$rand = int($rand) + 1;
|
||||
|
||||
my $rand2 = rand;
|
||||
$rand2 *= 10;
|
||||
$rand2 = int($rand2);
|
||||
|
||||
my @rand2 = ( '!', '@', '#', '$', '%', '^', '&', '*', '(', ')' );
|
||||
|
||||
# For debugging.
|
||||
# warn "SPLICE $rand2", $rand2[$rand2], "\n";
|
||||
|
||||
# Splice in number.
|
||||
$pwgen = substr( $pwgen, 0, $rand - 1 ) . $rand2[$rand2] . substr( $pwgen, $rand );
|
||||
}
|
||||
|
||||
# Add password for display.
|
||||
return $pwgen." (random password)";
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
1;
|
||||
|
|
|
@ -1,66 +1,66 @@
|
|||
package DDG::Goodie::RandomNumber;
|
||||
|
||||
use DDG::Goodie;
|
||||
|
||||
primary_example_queries 'random number between 1 and 12', 'random number';
|
||||
description 'generates a random number';
|
||||
name 'RandomNumber';
|
||||
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodies/RandomNumber.pm';
|
||||
category 'computing_tools';
|
||||
topics 'cryptography';
|
||||
attribution github => ['https://github.com/duckduckgo', 'duckduckgo'];
|
||||
|
||||
zci answer_type => 'rand';
|
||||
|
||||
triggers start => 'rand','random','number';
|
||||
|
||||
handle query_lc => sub {
|
||||
srand();
|
||||
# Random number.
|
||||
# q_check (as opposed to q_internal) Allows for decimals.
|
||||
if ( $_ =~ /^\!?(?:rand(?:om|)(?: num(?:ber|)|)(?: between|))( [\d\.]+|)(?: and|)( [\d\.]+|)$/i ) {
|
||||
|
||||
# For debugging.
|
||||
# warn qq($1\t$2);
|
||||
|
||||
my $start = $1 || 0;
|
||||
my $end = $2 || 0;
|
||||
|
||||
# Messes up decimals.
|
||||
# $start = int($start) if $start;
|
||||
# $end= int($end) if $end;
|
||||
|
||||
$start = 1000000000 if $start > 1000000000;
|
||||
$start = 0 if $start < 0;
|
||||
|
||||
$end = 1000000000 if $end > 1000000000;
|
||||
$end = 0 if $end < 0;
|
||||
|
||||
$end = $start if $end && $end < $start;
|
||||
|
||||
# For debugging.
|
||||
# warn qq($start\t$end);
|
||||
|
||||
my $rand = rand;
|
||||
|
||||
if ( $start && !$end ) {
|
||||
$rand *= $start;
|
||||
$rand = int($rand) + 1;
|
||||
|
||||
}
|
||||
elsif ( $start && $end && $start == $end ) {
|
||||
$rand = $start;
|
||||
|
||||
}
|
||||
elsif ( $start && $end ) {
|
||||
$rand *= ( $end - $start + 1 );
|
||||
$rand = int($rand) + $start;
|
||||
}
|
||||
|
||||
# Add IP for display.
|
||||
return $rand ." (random number)";
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
1;
|
||||
package DDG::Goodie::RandomNumber;
|
||||
|
||||
use DDG::Goodie;
|
||||
|
||||
primary_example_queries 'random number between 1 and 12', 'random number';
|
||||
description 'generates a random number';
|
||||
name 'RandomNumber';
|
||||
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodies/RandomNumber.pm';
|
||||
category 'computing_tools';
|
||||
topics 'cryptography';
|
||||
attribution github => ['https://github.com/duckduckgo', 'duckduckgo'];
|
||||
|
||||
zci answer_type => 'rand';
|
||||
|
||||
triggers start => 'rand','random','number';
|
||||
|
||||
handle query_lc => sub {
|
||||
srand();
|
||||
# Random number.
|
||||
# q_check (as opposed to q_internal) Allows for decimals.
|
||||
if ( $_ =~ /^\!?(?:rand(?:om|)(?: num(?:ber|)|)(?: between|))( [\d\.]+|)(?: and|)( [\d\.]+|)$/i ) {
|
||||
|
||||
# For debugging.
|
||||
# warn qq($1\t$2);
|
||||
|
||||
my $start = $1 || 0;
|
||||
my $end = $2 || 0;
|
||||
|
||||
# Messes up decimals.
|
||||
# $start = int($start) if $start;
|
||||
# $end= int($end) if $end;
|
||||
|
||||
$start = 1000000000 if $start > 1000000000;
|
||||
$start = 0 if $start < 0;
|
||||
|
||||
$end = 1000000000 if $end > 1000000000;
|
||||
$end = 0 if $end < 0;
|
||||
|
||||
$end = $start if $end && $end < $start;
|
||||
|
||||
# For debugging.
|
||||
# warn qq($start\t$end);
|
||||
|
||||
my $rand = rand;
|
||||
|
||||
if ( $start && !$end ) {
|
||||
$rand *= $start;
|
||||
$rand = int($rand) + 1;
|
||||
|
||||
}
|
||||
elsif ( $start && $end && $start == $end ) {
|
||||
$rand = $start;
|
||||
|
||||
}
|
||||
elsif ( $start && $end ) {
|
||||
$rand *= ( $end - $start + 1 );
|
||||
$rand = int($rand) + $start;
|
||||
}
|
||||
|
||||
# Add IP for display.
|
||||
return $rand ." (random number)";
|
||||
}
|
||||
return;
|
||||
};
|
||||
|
||||
1;
|
||||
|
|
Loading…
Reference in New Issue