Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Random generator fail

by blue_cowdawg (Prior)
on Dec 05, 2012 at 21:18 UTC ( #1007399=note: print w/ replies, xml ) Need Help??


in reply to Random generator fail

      But always get between 0 and 1. Any ideas?

Works as designed.

$ perl -e 'use 5.10.0; say int(6*rand())+1 foreach(1..10);' 6 5 4 6 2 2 4 6 3 5
Is that what you were after? Or at least something like it?

As MidLifeXis pointed out:

rand EXPR rand Returns a random fractional number greater than or equa +l to 0 and less than the value of EXPR. (EXPR should be posit +ive.) If EXPR is omitted, the value 1 is used. Currently EXP +R with the value 0 is also special-cased as 1 (this was undocu +mented before Perl 5.8.0 and is subject to change in future ve +rsions of Perl). Automatically calls "srand" unless "srand" h +as already been called. See also "srand". 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. (Note: If your rand function consistently returns numbe +rs that are too large or too small, then your version of Perl w +as probably compiled with the wrong number of RANDBITS.) "rand()" is not cryptographically secure. You should n +ot rely on it in security-sensitive situations. As of this wri +ting, a number of third-party CPAN modules offer random number generators intended by their authors to be cryptographi +cally secure, including: Math::Random::Secure, Math::Random::MT::Perl, and Math::TrulyRandom.


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg


Comment on Re: Random generator fail
Select or Download Code
Re^2: Random generator fail
by Anonymous Monk on Dec 05, 2012 at 22:41 UTC

    Thanks guys. I will take this as work around. But I should not be forced to use random integers, when I want decimals. Nowhere in documentation it says that rand be conditioned to return integers.

    Thank you.

      Have you read the reply? In case you have not, I am repeating the part you missed:
      Apply "int()" to the value returned by "rand()" if you want random integers instead of random fractional numbers
      No one forces you.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Please note that rand does not use $_ as an input value (which your original code seems to assume).

      You are absolutely correct - the documentation for rand does not state that it conditions the results as integers. That is done by the int call around the results of rand. Please read the fine documentation:

      Returns a random fractional number greater than or equal to 0 and less than the value of EXPR. (EXPR should be positive.) If EXPR is omitted, the value 1 is used. (emphasis added)
      Use rand EXPR if you want a value N where 0 <= N < EXPR. If you want an integer value, apply the int function to the results, if you want a floating point number (decimal), don't apply the int function. It appears to me that the rand function meets your stated requirements exactly, if the proper parameters are passed to it.

      If you need to scale the value over a range MIN..MAX (possibly even negative), then you want to use something like MIN + rand(abs(MAX-MIN)) (beware the boundary conditions - I may be off by one).

      See also:

      --MidLifeXis

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007399]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-08-28 10:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (259 votes), past polls