Re: rand()



On Mon, Jul 28, 2008 at 11:52 PM, Bobby <cybercruiserz@xxxxxxxxx> wrote:
Peng,



Could you give me an example code? I want to randomly select X numbers of
numbers from the @nums list. For instance, i want to randomly select 3
numbers from @nums i.e. 10000, 10005, 140000. How would you use srand to do
this?


Try the modified code below, it works fine.

use strict;
use warnings;
my $max=3;
my @nums = ("10000","10002","10004","10005","10006","140000","1500000");
my @randnum = map { $nums[int rand(@nums)] } 1 .. $max;
print "@randnum \n";


--
Regards,
Jeff. - Peng.Kyo@xxxxxxxxx
.



Relevant Pages

  • Re: rand()
    ... use strict; ... use warnings; ... if the size of the array increases just as above the duplication chance reduced or disappear completely. ...
    (perl.beginners)
  • Re: rand()
    ... I've ran a few tests and the code returned duplicate numbers, is there a way to have the script returns only unique values? ... use strict; ... use warnings; ...
    (perl.beginners)
  • Re: rand()
    ... Subject: rand() ... use strict; ... use warnings; ...
    (perl.beginners)
  • Re: Sharing variables between modules
    ... use strict; ... use warnings; ... # Note that this must also come *before* the use Foo line. ... package Foo; ...
    (comp.lang.perl.misc)
  • Re: Dynamic directory handles?
    ... > 1) You should always enable warnings (and strict) when ... > perldoc -q filehandle ... >> sub dircount { ... > use warnings; ...
    (comp.lang.perl.misc)