Re: rand()



Thanks Peng, that was what I was looking for. One more question, if i want to ensure true randomness instead of pseudo-random; would the Math::TruRandom module be my best option? Has any of you worked with that before. I'm install that module now and it's taking a long time to run a make test, has been 20 minutes already;  is that normal? Thanks.

--- On Mon, 7/28/08, peng.kyo@xxxxxxxxx <peng.kyo@xxxxxxxxx> wrote:

From: peng.kyo@xxxxxxxxx <peng.kyo@xxxxxxxxx>
Subject: Re: rand()
To: cybercruiserz@xxxxxxxxx
Cc: beginners@xxxxxxxx
Date: Monday, July 28, 2008, 9:58 AM

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)
  • randomly choose some uniq elements of an array
    ... use warnings; ... use strict; ... closedir DH; ... push(@shuffled, splice(@files, rand @files, 1)); ...
    (comp.lang.perl.misc)
  • Re: rand()
    ... Try the modified code below, ... use strict; ... use warnings; ...
    (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: warning: non-DWIM behavior
    ... > that's what anyone would want) into "rand" which somehow ends up the ... the 'use warnings' mantra will save you from self-inflicted ... > This is not the sort of DWIM behavior I normally expect from Perl! ... Perl couldn't really be expected to know what you meant. ...
    (comp.lang.perl.misc)