Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Random numbers generation

by Anonymous Monk
on May 21, 2004 at 16:42 UTC ( [id://355336]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am new to perl and I am trying to write a program that generates 100 random numbers between 250 and 500. Below is what I have done, but it is not working. Can someone clue me in to what I am doing wrong?
#!/usr/bin/perl # Generate 100 random numbers between 250 and 500 for ($x=250;$x<500;$x++) { if (($x<500) && ($x>250)) { print rand(100)."\t"; if ($x%5==0) { print "\n"; } } } END;

Edited by Chady -- added code tags.

Replies are listed 'Best First'.
Re: Random numbers generation
by pbeckingham (Parson) on May 21, 2004 at 16:47 UTC

    You are looping 250 times, generating numbers between 0 and 100. Try this:

    #! /usr/bin/perl -w use strict; print int (rand (251) + 250), "\n" for (1..100);

    Update: Thanks to blue_cowdawg and pizza_milkshake, changed the 250 to 251.

      this will generate numbers between 250 and 499 because of the behaviors of rand and int:

      rand Returns a random fractional number greater than or equal to 0 and less than the value of EXPR.

      int Returns the integer portion of EXPR. If EXPR is omitted, uses $_. You should not use this function for rounding: one because it trun-cates towards 0, and two because machine representations of floating point numbers can sometimes produce counterintuitive results.

      rand(250) will always return a number less than 250 and int will never round up.

      use rand(251)

      perl -e"\$_=qq/nwdd\x7F^n\x7Flm{{llql0}qs\x14/;s/./chr(ord$&^30)/ge;print"

      What do we mean by "between"? Your code works fine, but it includes 250 and excludes 500.

            Your code works fine, but it includes 250 and excludes 500.

        Direct quote from perldoc -f rand:

        Apply "int()" to the value returned by "rand()" if you +want random integers instead of random fractional numbers. +For example, int(rand(10)) returns a random integer between 0 and 9, inclusive.
        With this in mind take a look at my reply and apply a small tweak:
        my @answers=(); push @answers,(int(rand(251))+250) foreach (0..99);
        The sequence int(rand(251)) will now produce integers in the range of 0..250 and when added to 250 will produce 250..500.

        What I meant was that from reading the OP's code, it generates random numbers between 0 and 100, but does it 250 times. The algorithm is backwards.

Re: Random numbers generation
by Art_XIV (Hermit) on May 21, 2004 at 17:47 UTC

    TMTOWTDI, and here's a way to ensure that the same number isn't selected more than once:

    use strict; use warnings; use List::Util qw(shuffle); my @numbers = (shuffle(250 .. 500))[0 .. 99]; print "@numbers\n"
    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
Re: Random numbers generation
by PERLscienceman (Curate) on May 21, 2004 at 19:15 UTC
    Greetings Fellow Monk!

    Here is yet another way to do what you are asking for using the CPAN Module Math::Random.
    #!/usr/bin/perl -w use strict; use Math::Random; my($n,$low,$high)=(100,250,500); my @array=random_uniform_integer($n,$low,$high); for(my $i=0; $i<$n; $i++){print "$array[$i]\n";}
    $n is the number of random numbers you wish to generate,
    $low is the lower limit, $high is the upper limit, and
    @array is the array containing your random numbers as specified by your lower and upper limits.

    The beauty of Perl, The Perl Community, and CPAN is that there is usually a module out there that will do just about anything you are looking for often times significantly reducing the amount of code you need to write.
Re: Random numbers generation
by blue_cowdawg (Monsignor) on May 21, 2004 at 16:52 UTC

    Try this:

    my @answers=(); push @answers,(int(rand(250)+250) foreach (0..99);
    Completely untested...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://355336]
Approved by pbeckingham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 06:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found