Jump to content

Random numbers: Difference between revisions

added ruby
(added ruby)
Line 60:
 
=={{header|C}}==
<c>#include <stdlib.h>
#include <math.h>
 
double drand() /* uniform distribution, (0..1] */
{
return (rand()+1.0)/(RAND_MAX+1.0);
}
double random_normal() /* normal distribution, centered on 0, std dev 1 */
{
return sqrt(-2*log(drand())) * cos(2*M_PI*drand());
}
int main()
{
int i;
double rands[1000];
for (i=0; i<1000; i++)
rands[i] = 1.0 + 0.5*random_normal();
return 0;
}</c>
}
 
=={{header|C++}}==
<cpp>#include <cstdlib> // for rand
#include <cmath> // for atan, sqrt, log, cos
#include <algorithm> // for generate_n
 
double const pi = 4*std::atan(1.0);
 
// simple functor for normal distribution
class normal_distribution
{
public:
normal_distribution(double m, double s): mu(m), sigma(s) {}
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 r2 = (std::rand() + 1.0)/(RAND_MAX + 1.0);
return mu + sigma * std::sqrt(-2*std::log(r1))*std::cos(2*pi*r2);
}
private:
double mu, sigma;
};
 
int main()
{
double array[1000];
std::generate_n(array, 1000, normal_distribution(1.0, 0.5));
return 0;
}
}</cpp>
 
=={{header|E}}==
Line 193 ⟶ 194:
 
=={{header|Java}}==
<java>double[] list = new double[1000];
Random rng = new Random();
for(int i = 0;i<list.length;i++) {
list[i] = 1.0 + 0.5 * rng.nextGaussian()
}</java>
}
 
=={{header|JavaScript}}==
<javascript>function randomNormal() {
return Math.cos(2 * Math.PI * Math.random()) * Math.sqrt(-2 * Math.log(Math.random()));
}
 
var a = new Array(1000);
for (var i=0; i<a.length; i++)
a[i] = randomNormal() / 2 + 1;</javascript>
 
=={{header|Logo}}==
Line 233 ⟶ 234:
 
=={{header|OCaml}}==
<ocaml>let pi = 4. *. atan 1.;;
let random_gaussian () =
1. +. sqrt (-2. *. log (Random.float 1.)) *. cos (2. *. pi *. Random.float 1.);;
let a = Array.init 1000 (fun _ -> random_gaussian ());;</ocaml>
 
=={{header|Perl}}==
{{libheader|Math::Cephes}}
<perl>use Math::Cephes qw($PI);
 
map {
1.0 + 0.5 * sqrt (-2 * log rand) * cos (2 * $PI * rand)
} 1..1000</perl>
 
=={{header|PHP}}==
 
<php>$pi = pi(); // Set PI
$a = range(1,1000); // Create array
// Cycle array values
foreach(range(1,1000) as $i){
$a[$i] = 1 + sqrt(-2 * log(mt_rand())) * cos(2 * $pi * mt_rand());
}</php>
}
 
=={{header|Pop11}}==
Line 274 ⟶ 275:
=={{header|Python}}==
{{works with|Python|2.5}}
<python>import random
randList = [random.gauss(1, .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.
Line 282 ⟶ 283:
=={{header|R}}==
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}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.