- our of curiosity what exactly does the int() do? i figure rand() generates

a random number... but what about int()

At 04:33 PM 10/2/2002, you wrote:>Should, that's how most dice-rolling algorithms work :-)

[Non-text portions of this message have been removed]

>

>N

>

>

>

>

>

>"jake@..." <jake

>03/10/2002 09:07 AM

>Please respond to perl-beginner

>

>

> To: perl-beginner@yahoogroups.com

> cc:

> Subject: Re: [PBML] Random numbers again

>

>

>so, would the line "$random = int(1 + rand 100);" add a random number from

>

>0 to 99 and add one to it? then put the result in $random?

>

>

>At 08:10 AM 10/2/2002, you wrote:

> >Let's get serious.

> >

> >The rand() function if called without parameters returns a random

> >REAL number in interval <0,1). The different braces mean that the

> >interval contains 0, but doesn't contain 1. Or that the number will

> >be greater than or equal to zero and less than 1.

> >

> >What do we have to do if we want to get a real number in interval

> ><0,10)? Well that's easy, all we have to do is to multiply the number

> >we get from rand() by 10 each time. (Or use rand(10) ... which

> >basicaly does the multiplication for us.)

> >

> > rand(10)

> > rand() * 10

> >

> >

> >Now let's see what do we have to do if we want to get an INTEGER from

> >0 to 10 INCLUSIVE.

> >

> >First iteration: we know how to get a real number in range <0,10),

> >what do we get if we truncate the number? (truncate = strip anything

> >behind the decimal dot)

> >

> > int( rand() * 10 )

> >

> >Well ... if the random number was 0.1889 we get 0, if it was 1.756468

> >we got 1, ... but do we ever get 10? Well no. All the random real

> >numbers we get are smaller than 10, and the truncation always gives

> >you a number that's smaller or the same as the original (unless the

> >number is negative, but that's not the case here).

> >

> >So what we get are numbers from 0 to 9.

> >

> >

> >Second iteration: OK, so we get one number less than we want. What

> >does that mean? Well it means that we need a bigger range, we would

> >need real numbers from 0 to 11 (in interval <0,11) ! including the 0,

> >excluding the 11) :

> >

> > int( rand() * 11)

> >

> >Let's try what do we get:

> >

> > while ($i++ < 50) {

> > print int( rand() * 11), "\n";

> > }

> >

> >Hurrah :-)

> >

> >

> >Now on to another task. We want a random integer from 5 to 15

> >inclusive.

> >

> >First look at the task above, we needed numbers from 0 to 10, that

> >means 11 different numbers. Now we want numbers from 5 to 15, which

> >means 11 numbers as well. Fine, we can map one set of numbers to the

> >other. So if we get 0, let's return 5, if we get 1 lets return 6,

> >etc. That is lets just add 5 to whatever we get from the code we used

> >to solve the previous task:

> >

> > int( rand() * 11) + 5

> >

> >And let's try what do we get:

> >

> > while ($i++ < 50) {

> > print int( rand() * 11) + 5, "\n";

> > }

> >

> >Hey, we are done :-)

> >

> >Jenda

> >P.S.: Was this helpful enough?

> >== Jenda@... ==

><<http://Jenda.Krynicky.cz>http://Jenda.Krynicky.cz>http://Jenda.Krynicky.cz

>==

> >: What do people think?

> >What, do people think? :-)

> > -- Larry Wall in <199808071736.KAA12738@...>

> >

> >

> >Yahoo! Groups Sponsor

> >ADVERTISEMENT

> >

> >Unsubscribing info is here:

> ><<http://help.yahoo.com/help/us/groups/groups-32.html>http://help.yahoo.c

> om/help/us/groups/groups-32.html>http://help.yahoo.com/help/us/groups/groups-32.html

>

>

> >

> >

> >Your use of Yahoo! Groups is subject to the

> ><<http://docs.yahoo.com/info/terms/>http://docs.yahoo.com/info/terms/>Yah

> oo! Terms of Service.

>

>

>

>[Non-text portions of this message have been removed]

>

>

>

>Unsubscribing info is here:

><http://help.yahoo.com/help/us/groups/groups-32.html>http://help.yahoo.com/help/us/groups/groups-32.html

>

>

>Your use of Yahoo! Groups is subject to

><http://docs.yahoo.com/info/terms/>http://docs.yahoo.com/info/terms/

>

>

>

>

>

>

>

>_________________________________________________________________________________

>

>This email contains confidential information intended only for the person

>named above and may be subject to legal privilege. If you are not the

>intended recipient, any disclosure, copying or use of this information is

>prohibited. The Department provides no guarantee that this communication

>is free of virus or that it has not been intercepted or interfered with.

>If you have received this email in error or have any other concerns

>regarding its transmission, please notify Postmaster@...

>_________________________________________________________________________________

>

>

>Yahoo! Groups Sponsor

>ADVERTISEMENT

>

>Unsubscribing info is here:

><http://help.yahoo.com/help/us/groups/groups-32.html>http://help.yahoo.com/help/us/groups/groups-32.html

>

>

>Your use of Yahoo! Groups is subject to the

><http://docs.yahoo.com/info/terms/>Yahoo! Terms of Service.

- On Oct 2, jake@... said:

>our of curiosity what exactly does the int() do? i figure rand() generates

Try:

>a random number... but what about int()

% perldoc -f int

or

% perl -le '$x = rand 10; print $x; print int $x'

It truncates a number to its integer portion. *IT DOES NOT ROUND.*

--

Jeff "japhy" Pinyan japhy@... http://www.pobox.com/~japhy/

RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/

** Look for "Regular Expressions in Perl" published by Manning, in 2002 **

<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.

[ I'm looking for programming work. If you like my work, let me know. ]