Random numbers: Difference between revisions

Content added Content deleted
(added ruby)
Line 60: Line 60:


=={{header|C}}==
=={{header|C}}==
#include <stdlib.h>
<c>#include <stdlib.h>
#include <math.h>
#include <math.h>

double drand() /* uniform distribution, (0..1] */
double drand() /* uniform distribution, (0..1] */
{
{
return (rand()+1.0)/(RAND_MAX+1.0);
return (rand()+1.0)/(RAND_MAX+1.0);
}
}
double random_normal() /* normal distribution, centered on 0, std dev 1 */
double random_normal() /* normal distribution, centered on 0, std dev 1 */
{
{
return sqrt(-2*log(drand())) * cos(2*M_PI*drand());
return sqrt(-2*log(drand())) * cos(2*M_PI*drand());
}
}
int main()
int main()
{
{
int i;
int i;
double rands[1000];
double rands[1000];
for (i=0; i<1000; i++)
for (i=0; i<1000; i++)
rands[i] = 1.0 + 0.5*random_normal();
rands[i] = 1.0 + 0.5*random_normal();
return 0;
return 0;
}</c>
}


=={{header|C++}}==
=={{header|C++}}==
#include <cstdlib> // for rand
<cpp>#include <cstdlib> // for rand
#include <cmath> // for atan, sqrt, log, cos
#include <cmath> // for atan, sqrt, log, cos
#include <algorithm> // for generate_n
#include <algorithm> // for generate_n

double const pi = 4*std::atan(1.0);
double const pi = 4*std::atan(1.0);

// simple functor for normal distribution
// simple functor for normal distribution
class normal_distribution
class normal_distribution
{
{
public:
public:
normal_distribution(double m, double s): mu(m), sigma(s) {}
normal_distribution(double m, double s): mu(m), sigma(s) {}
double operator() // returns a single normally distributed number
double operator() // returns a single normally distributed number
{
{
double r1 = (std::rand() + 1.0)/(RAND_MAX + 1.0); // gives equal distribution in (0, 1]
double r1 = (std::rand() + 1.0)/(RAND_MAX + 1.0); // gives equal distribution in (0, 1]
double r2 = (std::rand() + 1.0)/(RAND_MAX + 1.0);
double r2 = (std::rand() + 1.0)/(RAND_MAX + 1.0);
return mu + sigma * std::sqrt(-2*std::log(r1))*std::cos(2*pi*r2);
return mu + sigma * std::sqrt(-2*std::log(r1))*std::cos(2*pi*r2);
}
}
private:
private:
double mu, sigma;
double mu, sigma;
};
};

int main()
int main()
{
{
double array[1000];
double array[1000];
std::generate_n(array, 1000, normal_distribution(1.0, 0.5));
std::generate_n(array, 1000, normal_distribution(1.0, 0.5));
return 0;
}
}</cpp>


=={{header|E}}==
=={{header|E}}==
Line 193: Line 194:


=={{header|Java}}==
=={{header|Java}}==
double[] list = new double[1000];
<java>double[] list = new double[1000];
Random rng = new Random();
Random rng = new Random();
for(int i = 0;i<list.length;i++) {
for(int i = 0;i<list.length;i++) {
list[i] = 1.0 + 0.5 * rng.nextGaussian()
list[i] = 1.0 + 0.5 * rng.nextGaussian()
}</java>
}


=={{header|JavaScript}}==
=={{header|JavaScript}}==
function randomNormal() {
<javascript>function randomNormal() {
return Math.cos(2 * Math.PI * Math.random()) * Math.sqrt(-2 * Math.log(Math.random()));
return Math.cos(2 * Math.PI * Math.random()) * Math.sqrt(-2 * Math.log(Math.random()));
}
}

var a = new Array(1000);
var a = new Array(1000);
for (var i=0; i<a.length; i++)
for (var i=0; i<a.length; i++)
a[i] = randomNormal() / 2 + 1;
a[i] = randomNormal() / 2 + 1;</javascript>


=={{header|Logo}}==
=={{header|Logo}}==
Line 233: Line 234:


=={{header|OCaml}}==
=={{header|OCaml}}==
let pi = 4. *. atan 1.;;
<ocaml>let pi = 4. *. atan 1.;;
let random_gaussian () =
let random_gaussian () =
1. +. sqrt (-2. *. log (Random.float 1.)) *. cos (2. *. pi *. Random.float 1.);;
1. +. sqrt (-2. *. log (Random.float 1.)) *. cos (2. *. pi *. Random.float 1.);;
let a = Array.init 1000 (fun _ -> random_gaussian ());;
let a = Array.init 1000 (fun _ -> random_gaussian ());;</ocaml>


=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|Math::Cephes}}
{{libheader|Math::Cephes}}
use Math::Cephes qw($PI);
<perl>use Math::Cephes qw($PI);

map {
map {
1.0 + 0.5 * sqrt (-2 * log rand) * cos (2 * $PI * rand)
1.0 + sqrt (-2 * log rand) * cos (2 * $PI * rand)
} 1..1000
} 1..1000</perl>


=={{header|PHP}}==
=={{header|PHP}}==


$pi = pi(); // Set PI
<php>$pi = pi(); // Set PI
$a = range(1,1000); // Create array
$a = range(1,1000); // Create array
// Cycle array values
// Cycle array values
foreach(range(1,1000) as $i){
foreach(range(1,1000) as $i){
$a[$i] = 1 + sqrt(-2 * log(mt_rand())) * cos(2 * $pi * mt_rand());
$a[$i] = 1 + sqrt(-2 * log(mt_rand())) * cos(2 * $pi * mt_rand());
}</php>
}


=={{header|Pop11}}==
=={{header|Pop11}}==
Line 274: Line 275:
=={{header|Python}}==
=={{header|Python}}==
{{works with|Python|2.5}}
{{works with|Python|2.5}}
import random
<python>import random
randList = [random.gauss(1, .5) for i in range(1000)]
randList = [random.gauss(1, .5) for i in range(1000)]
# or [ random.normalvariate(1, 0.5) for i in range(1000)]
# or [ random.normalvariate(1, 0.5) for i in range(1000)]</python>


Note that the ''random'' module in the Python standard library supports a number of statistical distribution methods.
Note that the ''random'' module in the Python standard library supports a number of statistical distribution methods.
Line 282: Line 283:
=={{header|R}}==
=={{header|R}}==
result <- rnorm(1000, mean=1, sd=0.5)
result <- rnorm(1000, mean=1, sd=0.5)

=={{header|Ruby}}==
<ruby>(1..1000).map { 1 + Math.sqrt(-2 * Math.log(rand)) * Math.cos(2 * Math::PI * rand) }</ruby>


=={{header|Tcl}}==
=={{header|Tcl}}==