generating random numbers in perl

M

mahurshi

i have been trying to generate random numbers in perl
and so far, i have been partially successful.

my code is as follows:

for ($i = 1; $i <= 100; $i++)
{
$test = rand(100000);
printf ("%e\n", $test);
}

i am pasting a sample output here:

1.710742e+04
9.492561e+04
3.755295e+04
6.329775e+04
8.602091e+04
8.700460e+03
5.051776e+04
3.425434e+04
8.534643e+03

as you can see, most of the output is in the order of 10^4 and 10^3

i was wondering if there is a way to increase the "swing" in the
randomness
of these numbers, so that i can really see a better mix of numbers of
different magnitudes.


Mahurshi Akilla
 
A

A. Sinan Unur

(e-mail address removed) wrote in @i39g2000cwa.googlegroups.com:
i have been trying to generate random numbers in perl
and so far, i have been partially successful.

my code is as follows:

for ($i = 1; $i <= 100; $i++)
{
$test = rand(100000);
printf ("%e\n", $test);
}

i am pasting a sample output here:

1.710742e+04
9.492561e+04
3.755295e+04
6.329775e+04
8.602091e+04
8.700460e+03
5.051776e+04
3.425434e+04
8.534643e+03

as you can see, most of the output is in the order of 10^4 and 10^3

i was wondering if there is a way to increase the "swing" in the
randomness of these numbers, so that i can really see a better mix of
numbers of different magnitudes.

So you want to bias the random number generator?

The probability of drawing x <= 1000 is 1% and the probability of
drawing x <= 100 is 0.1%.

Using the binomial formula, you can easily figure out the expected
number of draws under 1000 out of 100,000 numbers in 100 draws. Yes,
that is correct. The expected number of draws less than 1000 out of 100
draws from [0 .. 100,000) is exactly one.

Now, if you don't want the numbers to be independently and uniformly
distributed, then that's a different task. Decide what probability
distribution you want to use, and use it.

Sinan
 
X

xhoster

i have been trying to generate random numbers in perl
and so far, i have been partially successful.

my code is as follows:

for ($i = 1; $i <= 100; $i++)
{
$test = rand(100000);
printf ("%e\n", $test);
}
....

as you can see, most of the output is in the order of 10^4 and 10^3

It is hard to see why that should not be the case.

i was wondering if there is a way to increase the "swing" in the
randomness
of these numbers, so that i can really see a better mix of numbers of
different magnitudes.


Maybe this would be more to your liking:

$test = exp(rand()*11.5);


Xho
 
X

xhoster

just curious, is there a reason why you picked that ?

I choose exponentiation because it is by far the most natural way to
generate data that varies over many orders of magnitude. I choose 11.5
because that puts the maximum value at about 100_000 (exp(11.5) ~
98715.77), which is close to the maximum value possible under your original
script.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top