CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Randomly choose + or -

 



Warren Bell
Deleted

Dec 20, 2000, 6:39 PM

Post #1 of 4 (564 views)
Randomly choose + or - Can't Post

I'm writing a script where I need to randomly generate either a + (plus) sign or a - (minus) sign. Does anyone know an easy way to do this?

TIA



sleuth
Enthusiast / Moderator

Dec 20, 2000, 7:40 PM

Post #2 of 4 (561 views)
Re: Randomly choose + or - [In reply to] Can't Post

 This will work,


Code
@plusMinus=("+", "-"); 
$number = int(rand(2));
print "$plusMinus[$number]\n";

Sleuth



Kanji
User

Dec 20, 2000, 7:43 PM

Post #3 of 4 (561 views)
Re: Randomly choose + or - [In reply to] Can't Post

Depends how random you need it to be, but the following should suffice for simple needs ...

$sign = int rand(2) ? "+" : "-";



Warren Bell
Deleted

Dec 20, 2000, 8:44 PM

Post #4 of 4 (557 views)
Re: Randomly choose + or - [In reply to] Can't Post

Ok, thanks guys I'll try that out.


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives