Gossamer Forum
Quote Reply
rand()
This is weird. I have assigned an array, or 1 to 5, yet some how its sending back a 0! I am using the 2nd to last line to do the debugging, and when nothing is assigned, it just reports back the number, which is 0. Am I doing something wrong? Unsure

Code:
my $opts = Links::Plugins->get_plugin_user_cfg('Basic_Banner');

my @array = (1..5); # deifne the numbers we need for the array
my $rand_number = int(rand scalar(@array)); # get the random number...

my $text = $opts->{'Text_' . $rand_number};
my $text_url = $opts->{'Text_' . $rand_number . '_Image'};


my $html = "[ <a href=\"$text_url\" target=\"_new\"><font face=\"Tahoma\" style=\"font-size: 9pt\">$text</font></a> ]";
if (!$text_url) { $html = $rand_number; }
return $html;

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] rand() In reply to
Hello Andy,

rand is zero based.

So my $number = int(rand(5)); will produce possible numbers of 0,1,2,3,4

and my $number = 1 + int(rand(5)); will produce possible numbers of 1,2,3,4,5

Thanks

kode
Quote Reply
Re: [Andy] rand() In reply to
To get a random element of an array use:

my @random_array = (1 .. 5);
my $random_element = $random_array[rand @random_array];

Cheers,

Alex
--
Gossamer Threads Inc.