Two Plus Two Newer Archives

Two Plus Two Newer Archives (http://archives1.twoplustwo.com/index.php)
-   Probability (http://archives1.twoplustwo.com/forumdisplay.php?f=27)
-   -   Looking for event-based random number generator (http://archives1.twoplustwo.com/showthread.php?t=548103)

Ganjasaurus Rex 11-17-2007 02:15 AM

Looking for event-based random number generator
 
Sorry if this is the wrong forum. Wasn't sure where to put it.

Anyway, i'm looking for a random number generator that you can pre-program in the mean, SD, range, etc of n numbers. So in other words, let's say i want to generate 100 random numbers but want to make sure that the final mean and SD are predetermined. Is there such a program?

TIA

RustyBrooks 11-17-2007 03:38 AM

Re: Looking for event-based random number generator
 
This sparked a small memory in me and I did a quick investigation and it seems to hold.

You can actually generate a series of random numbers that are normally distributed by adding together 2 numbers that are randomly generated by a linear distribution. I remembered this because when you roll 2 dice, the sum of the dice is a standard distribution.

You can adjust to scale to the desired mean/standard deviation.

So, if you know any kind of programming language, go through a loop 100 times and do this:

n1 = rand()*std*2.45
n2 = rand()*std*2.45
num = mean + n1 + n2 - std*2.45

Why 2.45? I don't know. But it works.

I wrote a quick program for it. I could probably throw it up on the web if you're interested.

jukofyork 11-17-2007 03:54 AM

Re: Looking for event-based random number generator
 
[ QUOTE ]
This sparked a small memory in me and I did a quick investigation and it seems to hold.

You can actually generate a series of random numbers that are normally distributed by adding together 2 numbers that are randomly generated by a linear distribution. I remembered this because when you roll 2 dice, the sum of the dice is a standard distribution.

You can adjust to scale to the desired mean/standard deviation.

So, if you know any kind of programming language, go through a loop 100 times and do this:

n1 = rand()*std*2.45
n2 = rand()*std*2.45
num = mean + n1 + n2 - std*2.45

Why 2.45? I don't know. But it works.

I wrote a quick program for it. I could probably throw it up on the web if you're interested.

[/ QUOTE ]
I thought when I first read his post that he wanted to generate Gaussian random numbers too, but I think he wants to generate sequences that have an exact mean and standard deviation (eg: generate 10 random numbers that have a mean of -2.8 and a std of 1.5).

Juk [img]/images/graemlins/smile.gif[/img]

Ganjasaurus Rex 11-17-2007 04:05 AM

Re: Looking for event-based random number generator
 
[ QUOTE ]
[ QUOTE ]
This sparked a small memory in me and I did a quick investigation and it seems to hold.

You can actually generate a series of random numbers that are normally distributed by adding together 2 numbers that are randomly generated by a linear distribution. I remembered this because when you roll 2 dice, the sum of the dice is a standard distribution.

You can adjust to scale to the desired mean/standard deviation.

So, if you know any kind of programming language, go through a loop 100 times and do this:

n1 = rand()*std*2.45
n2 = rand()*std*2.45
num = mean + n1 + n2 - std*2.45

Why 2.45? I don't know. But it works.

I wrote a quick program for it. I could probably throw it up on the web if you're interested.

[/ QUOTE ]
I thought when I first read his post that he wanted to generate Gaussian random numbers too, but I think he wants to generate sequences that have an exact mean and standard deviation (eg: generate 10 random numbers that have a mean of -2.8 and a std of 1.5).

Juk [img]/images/graemlins/smile.gif[/img]

[/ QUOTE ]
I want to make a gaussian curve of random numbers with a predetermined mean and SD. Does Rusty's program do that?

RustyBrooks 11-17-2007 04:14 AM

Re: Looking for event-based random number generator
 
What I have will generate a sequence of numbers that have a given mean and standard distribution.

RustyBrooks 11-17-2007 04:15 AM

Re: Looking for event-based random number generator
 
(normal distribution = gaussian distribution, so I'm not entirely sure what you mean)

jukofyork 11-17-2007 04:25 AM

Re: Looking for event-based random number generator
 
[ QUOTE ]
[ QUOTE ]
[ QUOTE ]
This sparked a small memory in me and I did a quick investigation and it seems to hold.

You can actually generate a series of random numbers that are normally distributed by adding together 2 numbers that are randomly generated by a linear distribution. I remembered this because when you roll 2 dice, the sum of the dice is a standard distribution.

You can adjust to scale to the desired mean/standard deviation.

So, if you know any kind of programming language, go through a loop 100 times and do this:

n1 = rand()*std*2.45
n2 = rand()*std*2.45
num = mean + n1 + n2 - std*2.45

Why 2.45? I don't know. But it works.

I wrote a quick program for it. I could probably throw it up on the web if you're interested.

[/ QUOTE ]
I thought when I first read his post that he wanted to generate Gaussian random numbers too, but I think he wants to generate sequences that have an exact mean and standard deviation (eg: generate 10 random numbers that have a mean of -2.8 and a std of 1.5).

Juk [img]/images/graemlins/smile.gif[/img]

[/ QUOTE ]
I want to make a gaussian curve of random numbers with a predetermined mean and SD. Does Rusty's program do that?

[/ QUOTE ]
It will sample them from the Gaussian distribution and if you take enough samples (millions) the sampled mean/std will eventually approach the true mean/std you asked for, but it can't be used to select a fixed length sequence that gives an exact mean and std each time.

The only method I can think of is to just generate lots and lots of fixed length sequences (using a suitable Gaussian RNG generator) and discard the ones which don't have the exact mean and std you are looking for. If the ordering matters then you can always permute the order of (and reuse) the sequences using a shuffling algorithm (eg: Knuth Shuffle) as the mean and std won't be affected by order.

This link and the GSL source-code might also be helpful.

Juk [img]/images/graemlins/smile.gif[/img]

jukofyork 11-17-2007 04:28 AM

Re: Looking for event-based random number generator
 
[ QUOTE ]
(normal distribution = gaussian distribution, so I'm not entirely sure what you mean)

[/ QUOTE ]
Unless I've misread his OP, I think he wants to generate fixed length sequences which have a certain mean/std (as opposed to sampling from the Gaussian distribution).

Juk [img]/images/graemlins/smile.gif[/img]

RustyBrooks 11-17-2007 04:33 AM

Re: Looking for event-based random number generator
 
Exact? No. But for 100-length sequences it's OK. No 100-sized sample from a general population with a given mean and standard deviation will have that exact mean and standard deviation anyway. OK, so here are some samples from mean=50, std=25. When n=1000 the means are all within about 1, and std is within .4

mean == 46.1312, std == 25.0674
mean == 47.3159, std == 24.4849
mean == 46.4967, std == 25.8444
mean == 44.4332, std == 26.5974
mean == 48.8557, std == 26.0069
mean == 47.8602, std == 25.5480
mean == 52.0704, std == 27.5603
mean == 49.9019, std == 27.4975
mean == 52.4116, std == 24.7947
mean == 48.9171, std == 24.2495
mean == 50.5276, std == 22.1211
mean == 52.2813, std == 27.1429
mean == 49.0307, std == 22.4335
mean == 51.0344, std == 27.2197
mean == 50.5114, std == 24.4022
mean == 51.7506, std == 26.0787
mean == 48.8765, std == 27.6355
mean == 46.4424, std == 23.5208
mean == 44.5342, std == 24.2434
mean == 42.9070, std == 24.9680

RustyBrooks 11-17-2007 04:34 AM

Re: Looking for event-based random number generator
 
There's no way to get an exact mean/std in this way without doing what you suggested and rigorously weeding. I doubt that's what he wants though.

jukofyork 11-17-2007 04:38 AM

Re: Looking for event-based random number generator
 
[ QUOTE ]
There's no way to get an exact mean/std in this way without doing what you suggested and rigorously weeding. I doubt that's what he wants though.

[/ QUOTE ]
Yep, I wasn't sure if this is what he really wanted either, but it was just he said "want to make sure that the final mean and SD are predetermined".

Juk [img]/images/graemlins/smile.gif[/img]

R Gibert 11-17-2007 06:30 AM

Re: Looking for event-based random number generator
 
This is very easy. Generate a set of 100 random numbers with whatever distribution you want. Compute the std dev and mean for this sample.

Now compute the ratio of the desired std dev with the sample std dev. Use this ratio to scale the 100 numbers. This will coerce the sample to have the desired std dev. Note that the sample mean will also get scaled by this ratio.

Now to get the mean to where you want it. Compute the difference between the scaled sample mean and the desired mean. Subtract this difference from each of the 100 numbers. This will not alter the std dev, but it will coerce the mean to the desired value.

This procedure will coerce the original set of 100 random numbers to the exact mean and exact std dev desired if the numbers are real numbers. If you want integers, there will be some bias depending on the mean and std dev desired, but this bias can be minimized, by a slightly more involved procedure.

plexiq 11-17-2007 06:48 AM

Re: Looking for event-based random number generator
 
If i understand correctly, you want a gaussian looking sample with an exact mean/sd?

All you have to do is generate some kind of gaussian sample, and then transform it by:

sample_mean = mean(x in sample)
sample_sd = sd(x in sample)

for(x in sample)
x = (x-sample_mean)*desired_sd/sample_sd + desired_mean;

In R this would look something like that, for producing a sample of 10 with exact mean=10, sd=3:

<font class="small">Code:</font><hr /><pre>&gt; x=rnorm(10)
&gt; x
[1] 2.09314362 0.99306790 -0.87603143 1.26745925 -0.03479952 -1.35067764
[7] 0.80660410 0.38489743 -0.59618511 1.98929586
&gt; x = (x-mean(x))/sd(x)*3+10
&gt; x
[1] 14.139231 11.337901 6.578261 12.036635 8.720448 5.369580 10.863073
[8] 9.789202 7.290887 13.874783
&gt; mean(x)
[1] 10
&gt; sd(x)
[1] 3
</pre><hr />

Edit: Oh yay, Gibert just posted exactly the same. lol

BruceZ 11-17-2007 10:09 AM

Re: Looking for event-based random number generator
 
[ QUOTE ]
You can actually generate a series of random numbers that are normally distributed by adding together 2 numbers that are randomly generated by a linear distribution.

[/ QUOTE ]

Numbers generated this way are not normally distributed. You need to add more than 2 numbers in order to adequately approximate a normal distribution by the central limit theorem. Adding 2 uniformly distributed numbers gives a triangular shaped density function, not a normal bell curve.

[ QUOTE ]
I remembered this because when you roll 2 dice, the sum of the dice is a standard distribution.

[/ QUOTE ]

The sum of 2 dice follows the discreet version of the triangular shaped density function.


[ QUOTE ]
n1 = rand()*std*2.45
n2 = rand()*std*2.45
num = mean + n1 + n2 - std*2.45

[/ QUOTE ]

If rand() is the standard uniform distribution on [0,1], this will generate numbers with the given mean and standard deviation, but the density function will be triangular in shape.


[ QUOTE ]
Why 2.45? I don't know. But it works.

[/ QUOTE ]

The value 2.45 is an approximation for sqrt(6). Your formula can be written as

num = mean + sqrt(6)*std*[rand() + rand() - 1]

The variance of the uniform distribution rand() is 1/12, so the variance of rand() + rand() is 1/6. Subtracting 1 does not change the variance, so the standard deviation of the term in brackets is 1/sqrt(6), so multiplying by sqrt(6)*std gives the desired standard deviation.

This post gives a method for generating normally distributed random numbers from uniformly distributed random numbers if you have access to the inverse normal function as in Excel. Another method is to use the Box-Muller transformation. I have generated normally distributed random numbers by simply adding together a sufficiently large number of uniformly distributed random numbers (as from the rand() function).

If the OP requires that every set of n numbers he generates has the same mean and standard deviation, then he can use one of the other methods in this thread to scale the final numbers to achieve this. Normally distributed numbers would show some variation in the actual mean and standard deviation for each set of n numbers. These variations become small as n becomes large. The methods in this post will generate numbers which are distributed as a normal distribution with the desired mean and standard deviation, and the actual computed mean and standard deviation for each set of n numbers will vary as they should.

RustyBrooks 11-17-2007 01:05 PM

Re: Looking for event-based random number generator
 
Ah, OK, my bad. I don't know where I remembered it from, but my memory is kind of bad. The transformation stuff is interesting also.

Ganjasaurus Rex 11-17-2007 02:10 PM

Re: Looking for event-based random number generator
 
[ QUOTE ]
If i understand correctly, you want a gaussian looking sample with an exact mean/sd?

All you have to do is generate some kind of gaussian sample, and then transform it by:

sample_mean = mean(x in sample)
sample_sd = sd(x in sample)

for(x in sample)
x = (x-sample_mean)*desired_sd/sample_sd + desired_mean;

In R this would look something like that, for producing a sample of 10 with exact mean=10, sd=3:

<font class="small">Code:</font><hr /><pre>&gt; x=rnorm(10)
&gt; x
[1] 2.09314362 0.99306790 -0.87603143 1.26745925 -0.03479952 -1.35067764
[7] 0.80660410 0.38489743 -0.59618511 1.98929586
&gt; x = (x-mean(x))/sd(x)*3+10
&gt; x
[1] 14.139231 11.337901 6.578261 12.036635 8.720448 5.369580 10.863073
[8] 9.789202 7.290887 13.874783
&gt; mean(x)
[1] 10
&gt; sd(x)
[1] 3
</pre><hr />

Edit: Oh yay, Gibert just posted exactly the same. lol

[/ QUOTE ]

This is perfect! Thank you all for the help here. I really appreciate it! [img]/images/graemlins/smile.gif[/img]


All times are GMT -4. The time now is 05:15 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.