×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

I need help with randomly picking two items from an array.

I need help with randomly picking two items from an array.

I need help with randomly picking two items from an array.

(OP)
Hello PERL programmer world. Man I love PERL, too bad Larry Wall is deciding to stop the upgrading of it right?

Well anyway, on to my question. Ok, I am making a blackjack game in PERL, and I need help on randomly picking the card names from an array.

This is what I have set up so far

# this is where the card are picked from random

$house_cards = ("", @house_cards[ map { rand @house_cards} (5 .. 7) ]);

# the players cards, will also be picked from random
@player_cards = ("two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine ", "ten ", "jack ", "queen ", "king ", "ace ");

# types of cards, will be randomly matches with a randomly picked card number
@types = ("heart ", "diamond ", "club ", "spade ");



Ok, in case you are a little lostm all I have set up are two arrays with the names of all the types of cards that you can have a long with they card type, ie heart, diamond, etc. The scalar that says $house_cards is a variable that defines what cards to pick randomly, but the problem with that is it picks the same two cards everytime (and obviously that is not random whatsoever). I got that piece of code from the Perl Cookbook (by O'Reilly) which I think is the best PERL book ever, but it is not working for what I need it to do.

I have tried the rand() function and that fails without prevail also.

So, if anyone can help, I would be very appreciative.


Thanks a lot, and lets all give a shout out to all the kick @$$ PERL programmers out there!

Thanks a lot in advance,

Vic Cherubini
CEO V O I D Interactive.com
(site up soon)

RE: I need help with randomly picking two items from an array.

this should get you going, from the perl documentation, found with perldoc -q rand


# at the top of the program - only do this once
srand;                      # not needed for 5.004 and later
 
# then later on - to pick a random card
$index   = rand @array;
$card = $array[$index];


"too bad Larry Wall is deciding to stop the upgrading of it"

I didn't know that - where did you pick that up from?

Shout up again if you have more trouble

Mike
michael.j.lacey@ntlworld.com
Cargill's Corporate Web Site
Please -- Don't send me email questions without posting them in Tek-Tips as well. Better yet -- Post the question in Tek-Tips and send me a note saying "Have a look at so-and-so in the thingy forum would you?"

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close