2014-08-18 14:53:55 -07:00
|
|
|
package DDG::Goodie::RandomNumber;
|
2014-08-30 11:31:50 -07:00
|
|
|
# ABSTRACT: generate a random number in the requested range.
|
2014-08-18 14:53:55 -07:00
|
|
|
|
2015-02-22 12:09:29 -08:00
|
|
|
use strict;
|
2014-08-18 14:53:55 -07:00
|
|
|
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';
|
2015-01-07 10:24:47 -08:00
|
|
|
attribution github => ['duckduckgo', 'DuckDuckGo'];
|
2014-08-18 14:53:55 -07:00
|
|
|
|
|
|
|
zci answer_type => 'rand';
|
2014-09-27 06:42:57 -07:00
|
|
|
zci is_cached => 0;
|
2014-08-18 14:53:55 -07:00
|
|
|
|
|
|
|
triggers start => 'rand','random','number';
|
|
|
|
|
|
|
|
handle query_lc => sub {
|
|
|
|
srand();
|
|
|
|
# Random number.
|
|
|
|
# q_check (as opposed to q_internal) Allows for decimals.
|
2014-10-15 07:44:27 -07:00
|
|
|
return unless ($_ =~ /^\!?(?:rand(?:om|)(?: num(?:ber|)|)(?: between|))( [\d\.]+|)(?: and|)( [\d\.]+|)$/i);
|
2014-08-18 14:53:55 -07:00
|
|
|
|
2014-10-15 07:44:27 -07:00
|
|
|
my $start = $1 || 0;
|
|
|
|
my $end = $2 || 0;
|
2014-08-18 14:53:55 -07:00
|
|
|
|
2014-10-15 07:44:27 -07:00
|
|
|
$start = 1000000000 if $start > 1000000000;
|
|
|
|
$start = 0 if $start < 0;
|
|
|
|
$start += 0;
|
2014-08-18 14:53:55 -07:00
|
|
|
|
2014-10-15 07:44:27 -07:00
|
|
|
$end = 1000000000 if $end > 1000000000;
|
|
|
|
$end = 0 if $end < 0;
|
|
|
|
$end = 1 if !$end;
|
|
|
|
$end += 0;
|
2014-08-18 14:53:55 -07:00
|
|
|
|
2014-10-15 07:44:27 -07:00
|
|
|
($end, $start) = ($start, $end) if ($start > $end);
|
2014-08-18 14:53:55 -07:00
|
|
|
|
2014-10-15 07:44:27 -07:00
|
|
|
my $rand = rand;
|
2014-08-18 14:53:55 -07:00
|
|
|
|
2014-10-15 07:44:27 -07:00
|
|
|
if ($start && $end) {
|
|
|
|
$rand *= ($end - $start + 1);
|
|
|
|
$rand = int($rand) + $start;
|
2014-08-18 14:53:55 -07:00
|
|
|
}
|
2014-10-15 07:44:27 -07:00
|
|
|
|
|
|
|
return $rand . " (random number)",
|
|
|
|
structured_answer => {
|
|
|
|
input => [$start, $end],
|
2015-01-09 00:05:46 -08:00
|
|
|
operation => 'Random number between',
|
2014-10-15 07:44:27 -07:00
|
|
|
result => $rand
|
|
|
|
};
|
2014-08-18 14:53:55 -07:00
|
|
|
};
|
|
|
|
|
2014-08-30 11:31:50 -07:00
|
|
|
1;
|