Re: Random generator fail
by MidLifeXis (Monsignor) on Dec 05, 2012 at 21:10 UTC

 [reply] 
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
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 specialcased 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 securitysensitive situations. As of this wri
+ting, a
number of thirdparty 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
 [reply] [d/l] [select] 

 [reply] 

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.
 [reply] 

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(MAXMIN)) (beware the boundary conditions  I may be off by one).
See also:
 [reply] [d/l] [select] 
Re: Random generator fail
by BillKSmith (Priest) on Dec 06, 2012 at 04:34 UTC

 [reply] [d/l] 

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
...
But I think using additional line { say rand($_); is completely unneccessary.
Thank you.
 [reply] [d/l] [select] 

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.
 [reply] [d/l] [select] 


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.
 [reply] [d/l] [select] 

perl wle "use 5.10.0; say int foreach (1..100) ;"
Works as rand() should have.
 [reply] [d/l] 


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

$ perl E 'say 1 + int rand 99 for 1..3'
28
94
43
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
 [reply] [d/l] [select] 