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 Values

Status
Not open for further replies.

BenRussell

Programmer
Joined
Mar 12, 2001
Messages
243
Location
US
Say I have an array like the following :
@Values = ("3", "8", "2", "0", "23" , "5");
And I wanted to randomly select one of the numbers in there (These will not always be the numbers, but I am making an ad rotator program and these will refer to account numbers that contain banner information, etc.) How would I select one of the numbers and assign it to $SelectedValue?
Someone said to use
$SelectedValue = $Values[floor rand @Values];
But this gives an internal server error. I took out the floor, and it does not generate a random number, it picks the same number every time. - Ben Russell
- President of Intracor Technologies (
 
Try this. I'm sure it's not the cleanest way to do it, but it seems to work.

$array = @Values #To get the length of the array
$SelectedValue = int( rand($array)) #To generate the random number

- George
 
You can also use:
Code:
$SelectedValue = $Values[rand $#Values+1];
Using the scalar value of @Values like the above posting won't work very well because it will only return 0 if the array is empty (it's the NUMBER of elements in the array, not the subscript of the last value). $#Values will give you the subscript of the last value. rand will give you a fractional random number between 0 and the subscript of the last value of the array. Perl will automatically drop any fractional part in a subscript, so you don't need to worry about that, but in order to get it to use the last element, you've got to go one higher on the limit.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
That still doesn't work. I use the following code to display the ads :

@Values = ("1", "2");

$SelectedValue = $Values[rand $#Values+1];

if ($SelectedValue = "1") {
print "Content-type: text/html\n\n";
print <<&quot;EOF&quot;;

<HTML>
<HEAD><TITLE>Ad Server</TITLE></HEAD>
<BODY BGCOLOR=&quot;#FFFFFF&quot;>

<A HREF=&quot; TARGET=&quot;_top&quot;><IMG SRC=&quot; WIDTH=&quot;468&quot; HEIGHT=&quot;60&quot; BORDER=&quot;0&quot;></A>

EOF
} elsif ($SelectedValue = &quot;2&quot;) {
print &quot;Content-type: text/html\n\n&quot;;
print <<&quot;EOF&quot;;

<HTML>
<HEAD><TITLE>Ad Server</TITLE></HEAD>
<BODY BGCOLOR=&quot;#FFFFFF&quot;>

<a href=&quot; target=_top><img src=&quot; width=468 height=60 border=0 alt=&quot;Please click here to visit our sponsor&quot;></a>

EOF
}

--- End of code ---
But it only displays the first ad every time.
You can go to to see. - Ben Russell
- President of Intracor Technologies (
 
You need to change the if and elsif statements to state:

(els)if ($SelectedValue eq &quot;#&quot;) Where # is the value of the element in the array.

Your current code is always reassigning $SelectedValue to 1 when it hits the first if statement, causing your first ad to always display.

I did run your code with these changes through my webserver and they did work, showing both ads randomly.

Hope this helps!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top