Velocity Reviews > Liinear distribution of random numbers?

# Liinear distribution of random numbers?

petertwocakes
Guest
Posts: n/a

 01-17-2009
Hi

Is there a way of creating a distribution of random numbers (using
random() or any otherwise) that approximates:

frequency = N-x (0<=x<=N)

or

frequency = N/(x+1) (0<=x<=N)

Thanks

Steve

geo
Guest
Posts: n/a

 01-17-2009
On Jan 17, 7:16*am, petertwocakes <(E-Mail Removed)>
wrote:
> Is there a *way of creating a distribution *of random numbers (using
> random() or any otherwise) that approximates:
> frequency = N-x (0<=x<=N)
> or
> frequency = N/(x+1) *(0<=x<=N)
> Thanks
> Steve

Perhaps the fastest way to generate
such variates with exactly those (scaled)
frequencies---and with specific C programs---may
be found in a web-available article:
"Fast Generation of Discrete Random Variables",
Marsaglia, Tsang and Wang,
www.jstatsoft.org/v11/i03/paper

Ben Bacarisse
Guest
Posts: n/a

 01-17-2009
"Malcolm McLean" <(E-Mail Removed)> writes:

> "petertwocakes" <(E-Mail Removed)> wrote in message news:
>> Hi
>>
>> Is there a way of creating a distribution of random numbers (using
>> random() or any otherwise) that approximates:
>>
>> frequency = N-x (0<=x<=N)
>>
>> or
>>
>> frequency = N/(x+1) (0<=x<=N)
>>

>
> generate a uniform random number on 0-1 by
>
> rand()/(RAND_MAX + 1.0)
>
> Then log transform it
>
> x = (int) (log(uniform() * (e-1) + 1.0) * N)

.... and you hope the C implementer of rand() has not joined your
campaign for 64 bit ints!

--
Ben.

petertwocakes
Guest
Posts: n/a

 01-17-2009
On 17 Jan, 13:24, geo <(E-Mail Removed)> wrote:
> On Jan 17, 7:16*am, petertwocakes <(E-Mail Removed)>
> wrote:
>
> > Is there a *way of creating a distribution *of random numbers (using
> > random() or any otherwise) that approximates:
> > frequency = N-x (0<=x<=N)
> > or
> > frequency = N/(x+1) *(0<=x<=N)
> > Thanks
> > Steve

>
> Perhaps the fastest way to generate
> such variates with exactly those (scaled)
> frequencies---and with specific C programs---may
> be found in a web-available article:
> "Fast Generation of Discrete Random Variables",
> *Marsaglia, Tsang and Wang,www.jstatsoft.org/v11/i03/paper

petertwocakes
Guest
Posts: n/a

 01-17-2009
On 17 Jan, 13:50, "Malcolm McLean" <(E-Mail Removed)> wrote:
> "petertwocakes" <(E-Mail Removed)> wrote in message news:
> > Hi

>
> > Is there a *way of creating a distribution *of random numbers (using
> > random() or any otherwise) that approximates:

>
> > frequency = N-x (0<=x<=N)

>
> > or

>
> > frequency = N/(x+1) *(0<=x<=N)

>
> generate a uniform random number on 0-1 by
>
> rand()/(RAND_MAX + 1.0)
>
> Then log transform it
>
> x = (int) (log(uniform() ** (e-1) + 1.0) * N)
>
> (e is Euler's number to put your random number into the range 0-N-1).
>
> You can also square root transform it, or use a cosine transform.
>
> I am not sure what the best approximation to a linearly increasing density
> function actually is. If you do several thousand of them and plot out the
> histogram you can see what is acceptable.
>
> --
> Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm

>x = (int) (log(uniform() * (e-1) + 1.0) * N)

With a million samples, that gives something almost linear with just a
bit of sag(?); not sure what's happening yet, but it's a good starting
point for further exploration.

So far, this gives a reasonable linear:
r = random()% (int)sqrt(random()%((N*N)-1) +1);
and reciprocal:
r = random()%(random()%(N-1)+1);

Not very efficient though.

Steve

Walter Banks
Guest
Posts: n/a

 01-19-2009

geo wrote:

> On Jan 17, 7:16 am, petertwocakes <(E-Mail Removed)>
> wrote:
> > Is there a way of creating a distribution of random numbers (using
> > random() or any otherwise) that approximates:
> > frequency = N-x (0<=x<=N)
> > or
> > frequency = N/(x+1) (0<=x<=N)
> > Thanks
> > Steve

>
> Perhaps the fastest way to generate
> such variates with exactly those (scaled)
> frequencies---and with specific C programs---may
> be found in a web-available article:
> "Fast Generation of Discrete Random Variables",
> Marsaglia, Tsang and Wang,
> www.jstatsoft.org/v11/i03/paper

A very good practical reference I use for generating random
numbers with various distributions is,

"Statistical Distributions" N.A.J. Hastings and J.B.Peacock
ISBN 0-470-35889-0 QA273.6.H37
Distributed by Wiley 1974

There are later editions. This book is worth tracking down
for anyone who needs good random number generators.

Regards,

--
Walter Banks
Byte Craft Limited
1 519 888 6911
http://bytecraft.com
http://www.velocityreviews.com/forums/(E-Mail Removed)

Ben Bacarisse
Guest
Posts: n/a

 01-19-2009
petertwocakes <(E-Mail Removed)> writes:

> Is there a way of creating a distribution of random numbers (using
> random() or any otherwise) that approximates:
>
> frequency = N-x (0<=x<=N)

The function you want is pow(rand_in_0_1(), 0.5) * N though the
suggestion to use a table (as in the Marsaglia, Tsang and Wang paper)
may well be faster.

> frequency = N/(x+1) (0<=x<=N)

I think this one is pow(rand_in_0_1(), 2.0) * N. It may be too late
now, but I have found this a very useful reference:

http://ftp.arl.mil/random/random.pdf

--
Ben.

CBFalconer
Guest
Posts: n/a

 01-20-2009
Walter Banks wrote:
>

.... snip ...
>
> A very good practical reference I use for generating random
> numbers with various distributions is,
>
> "Statistical Distributions" N.A.J. Hastings and J.B.Peacock
> ISBN 0-470-35889-0 QA273.6.H37
> Distributed by Wiley 1974
>
> There are later editions. This book is worth tracking down
> for anyone who needs good random number generators.

Huh? How can you consider a 'random sequence' with 'controlled
distribution' to be a random number sequence? Or do those phrases
not mean what I attribute to them?

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

osmium
Guest
Posts: n/a

 01-20-2009
"CBFalconer" wrote:

> Walter Banks wrote:
>>

> ... snip ...
>>
>> A very good practical reference I use for generating random
>> numbers with various distributions is,
>>
>> "Statistical Distributions" N.A.J. Hastings and J.B.Peacock
>> ISBN 0-470-35889-0 QA273.6.H37
>> Distributed by Wiley 1974
>>
>> There are later editions. This book is worth tracking down
>> for anyone who needs good random number generators.

>
> Huh? How can you consider a 'random sequence' with 'controlled
> distribution' to be a random number sequence? Or do those phrases
> not mean what I attribute to them?

They are random drawings from a specified distribution. For a normal
distribution centered on zero, numbers near zero are more likely than
numbers two or three sigma away from zero.

Ben Bacarisse
Guest
Posts: n/a

 01-20-2009
CBFalconer <(E-Mail Removed)> writes:

> Walter Banks wrote:
>>

> ... snip ...
>>
>> A very good practical reference I use for generating random
>> numbers with various distributions is,
>>
>> "Statistical Distributions" N.A.J. Hastings and J.B.Peacock
>> ISBN 0-470-35889-0 QA273.6.H37
>> Distributed by Wiley 1974
>>
>> There are later editions. This book is worth tracking down
>> for anyone who needs good random number generators.

>
> Huh? How can you consider a 'random sequence' with 'controlled
> distribution' to be a random number sequence?

The numbers that result from calling rand() are usually a good
approximation to sampling from a discrete uniform distribution over
[O, RAND_MAX]. By computing some function of the these numbers it is
possible to approximate other distributions. For example,

int bin(void)
{
int r = rand();
return !(r & 1) + !(r & 2) + !(r & 4) + !(r & + !(r & 16);
}

will return a pseudo-random number with a binomial distribution.
Controlling the distribution to some extent does not prevent the
sequence from being pseudo-random.

> Or do those phrases
> not mean what I attribute to them?

If you said what you mean by them we'd know!

--
Ben.