Problems? Is your data what you think it is? PerlMonks

### Random generator fail

 on Dec 05, 2012 at 20:59 UTC Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying generate 100 random numbers using forach

between 1 thru 99

But always get between 0 and 1. Any ideas?

```perl -wle "use 5.10.0;  say  rand foreach(1..100) ;"

[download]```

Thanks.

Replies are listed 'Best First'.
Re: Random generator fail
by MidLifeXis (Monsignor) on Dec 05, 2012 at 21:10 UTC

See the documentation for rand. It explains pretty well why this is happening.

--MidLifeXis

Re: Random generator fail
by blue_cowdawg (Monsignor) on Dec 05, 2012 at 21:18 UTC
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

[download]```
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.

[download]```

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

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

Re: Random generator fail
by BillKSmith (Vicar) on Dec 06, 2012 at 04:34 UTC

Do you want real numbers in the range (1 <= number < 99) on a windows system?

perl -e"print 1 + rand 98, qq(\n) for 1..100"
Bill

Ok I have better understanding now, Bill your solutions works great!

@MidLifeXis What is rational for rand to ignore \$_. Appearently you pointing out that, solves my problem by using

```perl -wle "use 5.10.0;  foreach (1..100) { say rand(\$_);};"

1.94992065429688
56.7288208007813
52.1597290039063
18.6995849609375
6.6961669921875
68.6776733398438
65.6177978515625
62.72314453125
52.1324157714844
...
[download]```

But I think using additional line  { say rand(\$_); is completely un-neccessary. Thank you.

Let's step through this:

```foreach (1..100) {
say rand(\$_);
}
[download]```
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)
[download]```
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

My rand 98 generates random numbers in the range (0<= number < 98). Adding one to every random number changes the range to (1<=number<99). Using rand \$_ would generate a very different sequence of numbers. In fact, the sample output that you show cannot be the first few numbers as your dots suggest. It is possible that they are the last few numbers.
Bill

forgot to add this:

```perl -wle "use 5.10.0;   say int foreach (1..100) ;"
[download]```

Works as rand() should have.

Re: Random generator fail
by kcott (Chancellor) on Dec 07, 2012 at 09:03 UTC

Here's my \$0.02 worth:

```\$ perl -E 'say 1 + int rand 99 for 1..3'
28
94
43
[download]```

Obviously, change the 3 to 100.

Update: You appear to want decimals although you asked for a range using integers. Perhaps you could clarify.

This gives: 1.0 < random_number < 99.0

```\$ perl -E 'say 1 + rand 98 for 1..3'
38.6240726329874
72.0054041794683
94.0423383452302
[download]```

-- Ken

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1007396]
Approved by astroboy
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2018-02-18 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When it is dark outside I am happiest to see ...

Results (253 votes). Check out past polls.

Notices?