in reply to Random numbers generation

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.

Replies are listed 'Best First'.
Re: Re: Random numbers generation
by pizza_milkshake (Monk) on May 21, 2004 at 16:58 UTC
    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"

Re: Re: Random numbers generation
by Anonymous Monk on May 21, 2004 at 16:55 UTC
    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.