Hello everyone,
I want to generate random integers in Perl, to be used as
array indexes. Let's say my array size is 10, so I want to
get evenly distributed integers between 0 and 9.
If I use
$index = int(rand(10));
is there a chance (however slight) that I will eventually
get 10 as $index ? That would be out of bounds, and we all
hate that... Of course, something like
if($index == 10) { $index = 9; }
will fix it but it hurts my feelings about elegant programming )
So the bottom line of my question is : can rand(n) ever
return n in Perl (or PHP) ?
PS : You can see a distribution of 100000 calls to rand()
at : http://ixedix.x10hosting.com/
(hit the "rand() test" button at bottom, then refresh
to get new distributions)
PPS : Where is the darn "Close thread" checkbox that the
sticky message begs us to use ? Can't find it on my browser.
I want to generate random integers in Perl, to be used as
array indexes. Let's say my array size is 10, so I want to
get evenly distributed integers between 0 and 9.
If I use
$index = int(rand(10));
is there a chance (however slight) that I will eventually
get 10 as $index ? That would be out of bounds, and we all
hate that... Of course, something like
if($index == 10) { $index = 9; }
will fix it but it hurts my feelings about elegant programming )
So the bottom line of my question is : can rand(n) ever
return n in Perl (or PHP) ?
PS : You can see a distribution of 100000 calls to rand()
at : http://ixedix.x10hosting.com/
(hit the "rand() test" button at bottom, then refresh
to get new distributions)
PPS : Where is the darn "Close thread" checkbox that the
sticky message begs us to use ? Can't find it on my browser.