Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^3: Random generator fail

by MidLifeXis (Monsignor)
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.


Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007633]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-03-25 01:49 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (299 votes). Check out past polls.