Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Selection

Status
Not open for further replies.

BenRussell

Programmer
Joined
Mar 12, 2001
Messages
243
Location
US
Say i have an array that looks like
@Values = ("1", "2", "3", "4");

How do I select a random one of these and assign it to
"$SelectedValue"?

This will not necessarily be the numbers as in the actual script they will change depending on certain other things, so how would I do this?

Thank you.
- Ben Russell
- President of Intracor Technologies (
 
use POSIX;

$SelectedValue = $Values[floor rand @Values]; Brad Gunsalus
Cymtec Systems, Inc.
bgunsalus@cymtec.com
 
I would use the following:


@Values = qw(1 2 3 4);

$rand_value = $values[rand @Values];

print $rand_value;

Basically same as bardley:)
 
Yup. Use his.

Only wrote mine using floor() because I wasn't sure what perl would make of something like myarray[3.86572300276...] instead of myarray[3], but I should've known Perl would know what to do!

Cheers :-) Brad Gunsalus
Cymtec Systems, Inc.
bgunsalus@cymtec.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top