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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

vcherubini

Programmer
Joined
May 29, 2000
Messages
527
Location
US
Hello PERL programmer world. Man I love PERL, too bad Larry Wall is deciding to stop the upgrading of it right?<br><br>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.<br><br>This is what I have set up so far<br><br><FONT FACE=monospace># this is where the card are picked from random <br><br>$house_cards = (&quot;&quot;, @house_cards[ map { rand @house_cards} (5 .. 7) ]);<br><br># the players cards, will also be picked from random<br> @player_cards = (&quot;two &quot;, &quot;three &quot;, &quot;four &quot;, &quot;five &quot;, &quot;six &quot;, &quot;seven &quot;, &quot;eight &quot;, &quot;nine &quot;, &quot;ten &quot;, &quot;jack &quot;, &quot;queen &quot;, &quot;king &quot;, &quot;ace &quot;);<br><br># types of cards, will be randomly matches with a randomly picked card number<br> @types = (&quot;heart &quot;, &quot;diamond &quot;, &quot;club &quot;, &quot;spade &quot;);<br><br></font><br><br>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.<br><br>I have tried the <FONT FACE=monospace>rand()</font> function and that fails without prevail also.<br><br>So, if anyone can help, I would be very appreciative.<br><br><br>Thanks a lot, and lets all give a shout out to all the kick @$$ PERL programmers out there! <br><br>Thanks a lot in advance,<br><br>Vic Cherubini<br>CEO V O I D Interactive.com<br>(site up soon)
 
this should get you going, from the perl documentation, found with <FONT FACE=monospace>perldoc -q rand</font><br><br><FONT FACE=monospace><b><br># at the top of the program - only do this once<br>srand;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# not needed for 5.004 and later<br>&nbsp;<br># then later on - to pick a random card<br>$index&nbsp;&nbsp;&nbsp;= rand @array;<br>$card = $array[$index];<br></font></b><br><br>&quot;too bad Larry Wall is deciding to stop the upgrading of it&quot;<br><br>I didn't know that - where did you pick that up from?<br><br>Shout up again if you have more trouble<br> <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>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?"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top