View Single Post
  #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