Two Plus Two Newer Archives  

Go Back   Two Plus Two Newer Archives > General Gambling > Probability
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #11  
Old 11-17-2007, 04:38 AM
jukofyork jukofyork is offline
Senior Member
 
Join Date: Sep 2004
Location: Leeds, UK.
Posts: 2,551
Default 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]
Reply With Quote
  #12  
Old 11-17-2007, 06:30 AM
R Gibert R Gibert is offline
Member
 
Join Date: Jan 2006
Posts: 53
Default 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.
Reply With Quote
  #13  
Old 11-17-2007, 06:48 AM
plexiq plexiq is offline
Senior Member
 
Join Date: Apr 2007
Location: Vienna
Posts: 138
Default 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
Reply With Quote
  #14  
Old 11-17-2007, 10:09 AM
BruceZ BruceZ is offline
Senior Member
 
Join Date: Sep 2002
Posts: 4,078
Default 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.
Reply With Quote
  #15  
Old 11-17-2007, 01:05 PM
RustyBrooks RustyBrooks is offline
Senior Member
 
Join Date: Feb 2006
Location: Austin, TX
Posts: 1,380
Default 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.
Reply With Quote
  #16  
Old 11-17-2007, 02:10 PM
Ganjasaurus Rex Ganjasaurus Rex is offline
Senior Member
 
Join Date: Sep 2007
Posts: 336
Default 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]
Reply With Quote
Reply


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

Forum Jump


All times are GMT -4. The time now is 06:08 PM.


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