This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -8,7 +8,7 @@ int main()
{
random_device seed;
mt19937 engine(seed());
normal_distribution<> dist(1.0, 0.5);
normal_distribution<double> dist(1.0, 0.5);
auto rnd = bind(dist, engine);
vector<double> v(1000);

View file

@ -3,22 +3,23 @@ import std.stdio, std.random, std.math;
struct NormalRandom {
double mean, stdDev;
// needed because it also defines an opCall
// Necessary because it also defines an opCall.
this(in double mean_, in double stdDev_) pure nothrow {
this.mean = mean_;
this.stdDev = stdDev_;
}
double opCall() const /*nothrow*/ {
immutable double r1 = uniform(0.0, 1.0);
immutable double r2 = uniform(0.0, 1.0);
return mean + stdDev * sqrt(-2 * log(r1)) * cos(2 * PI * r2);
immutable r1 = uniform(0.0, 1.0), // Not nothrow.
r2 = uniform(0.0, 1.0);
return mean + stdDev * sqrt(-2 * r1.log) * cos(2 * PI * r2);
}
}
void main() {
double[1000] array;
auto nrnd = NormalRandom(1.0, 0.5);
auto nRnd = NormalRandom(1.0, 0.5);
foreach (ref x; array)
x = nrnd();
//x = nRnd;
x = nRnd();
}

View file

@ -0,0 +1,14 @@
import math, strutils
const precisn = 5
var rs: TRunningStat
proc normGauss: float {.inline.} = 1 + 0.76 * cos(2*PI*random(1.0)) * sqrt(-2*log10(random(1.0)))
randomize()
for j in 0..5:
for i in 0..1000:
rs.push(normGauss())
echo("mean: ", $formatFloat(rs.mean,ffDecimal,precisn),
" stdDev: ", $formatFloat(rs.standardDeviation(),ffDecimal,precisn))

View file

@ -2,4 +2,8 @@ sub randnorm ($mean, $stddev) {
$mean + $stddev * sqrt(-2 * log rand) * cos(2 * pi * rand)
}
my @nums = map { randnorm 1, 0.5 }, ^1000;
my @nums = randnorm(1, 0.5) xx 1000;
# Checking
say my $mean = @nums R/ [+] @nums;
say my $stddev = sqrt $mean**2 R- @nums R/ [+] @nums X** 2;

View file

@ -0,0 +1,12 @@
/* Generate 1000 random numbers with mean 1 and standard deviation 0.5.
SAS version 9.2 was used to create this code.*/
data norm1000;
call streaminit(123456);
/* Set the starting point, so we can replicate results.
If you want different results each time, comment the above line. */
do i=1 to 1000;
r=rand('normal',1,0.5);
output;
end;
run;

View file

@ -0,0 +1,2 @@
for (%i = 0; %i < 1000; %i++)
%list[%i] = 1 + mSqrt(-2 * mLog(getRandom())) * mCos(2 * $pi * getRandom());