Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Liinear distribution of random numbers?

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
geo
Guest
Posts: n/a
 
      01-17-2009
On Jan 17, 7:16*am, petertwocakes <petertwoca...@googlemail.com>
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


 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-17-2009
"Malcolm McLean" <> writes:

> "petertwocakes" <> 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.
 
Reply With Quote
 
petertwocakes
Guest
Posts: n/a
 
      01-17-2009
On 17 Jan, 13:24, geo <gmarsag...@gmail.com> wrote:
> On Jan 17, 7:16*am, petertwocakes <petertwoca...@googlemail.com>
> 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


Thanks, interesting read.
 
Reply With Quote
 
petertwocakes
Guest
Posts: n/a
 
      01-17-2009
On 17 Jan, 13:50, "Malcolm McLean" <regniz...@btinternet.com> wrote:
> "petertwocakes" <petertwoca...@googlemail.com> 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
 
Reply With Quote
 
Walter Banks
Guest
Posts: n/a
 
      01-19-2009


geo wrote:

> On Jan 17, 7:16 am, petertwocakes <petertwoca...@googlemail.com>
> 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
Halstead Press
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





 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-19-2009
petertwocakes <> 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.
 
Reply With Quote
 
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
> Halstead Press
> 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>
Try the download section.
 
Reply With Quote
 
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
>> Halstead Press
>> 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.


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-20-2009
CBFalconer <> 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
>> Halstead Press
>> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57