Re: rand()



Aruna,
 
That helps but how would i just get back an X number of random numbers because @nums could be in the hundreds of unique numbers. For instance, i just want to randomly pickout 4 numbers from a long list of unique numbers in @nums, how would i do that? Thanks.  

--- On Mon, 7/28/08, Aruna Goke <myklass@xxxxxxxxx> wrote:

From: Aruna Goke <myklass@xxxxxxxxx>
Subject: Re: rand()
To: "Perl beginners" <beginners@xxxxxxxx>
Date: Monday, July 28, 2008, 9:54 AM

Bobby wrote:
Hi all,

How do I use the rand function to print out more than one random number
for an array? In the example script below i have an array @nums with a list of
numbers; how do i print out more than one random numbers from that list
(@nums)? Thanks.

#!/usr/bin/perl
use strict;
use warnings;
my @nums =
("10000","10002","10004","10005","10006","140000","1500000");
my $randnum = $nums[rand(@nums)];
print "$randnum \n";






#!/usr/bin/perl

use strict;
use warnings;
my @nums =
("10000","10002","10004","10005","10006","140000","1500000");
print "@nums[0..rand(@nums)]\n";

--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/




Relevant Pages

  • Re: rand()
    ... use warnings; ... print "$randnum \n"; ... for an array? ... You may need srand before the rand function. ...
    (perl.beginners)
  • Re: please debug this generic program
    ... return sum; ... public static void main{ ... `nums' is an array, and an array has no method ...
    (comp.lang.java.programmer)
  • Re: Help with String Functions
    ... INSERT INTO Nums VALUES ... -- Function that splits an array into elements ... FROM (SELECT @arr AS string) AS Arrays JOIN Nums ...
    (microsoft.public.sqlserver.programming)
  • Re: Ignoring charachter data with Product function
    ... array enteredinstead of just enter ... > characters. ... > chars to nums has not done the trick. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: rand()
    ... Newsgroups: perl.beginners ... How do I use the rand function to print out more than one random number for an array? ... You can use 'shuffle' from List::Util. ...
    (perl.beginners)