Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: Random generator fail

by MidLifeXis (Prior)
on Dec 06, 2012 at 18:44 UTC ( #1007633=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Random generator fail
in thread Random generator fail

Let's step through this:

foreach (1..100) { say rand($_); }
When $_ is equal to ...
  • ... 1, rand($_) cannot be larger than 1.
  • ... 2, rand($_) cannot be larger than 2.
  • ... 3, rand($_) cannot be larger than 3.
  • ... 4, rand($_) cannot be larger than 4.
  • ... 5, rand($_) cannot be larger than 5.
  • ... 6, rand($_) cannot be larger than 6.

Look at one of my earlier posts on this thread. The documentation for rand states that 0 <= rand($x) < $x if $x > 0. To accomplish what you are trying to do would require that you do this:

say 1 + rand(99 - 1) for (1..100)
In my previous node, MAX = 99 and MIN = 1. The 1..100 is the number of iterations you wish to use. If you use rand($_), your results will be biased toward the lower end of the counter range.

--MidLifeXis


Comment on Re^3: Random generator fail
Select or Download Code
Re^4: Random generator fail
by Anonymous Monk on Dec 06, 2012 at 20:12 UTC

    I agree with bias at lower end. Great, that is pretty much what I was looking for.

    Thank you

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (12)
As of 2014-04-24 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (565 votes), past polls