Random numbers: Difference between revisions
Content added Content deleted
(convert to Header template) |
|||
Line 3: | Line 3: | ||
The goal of this task is to generate a 1000-element array (vector, list, whatever it's called in your language) filled with normally distributed random numbers with a mean of 1.0 and a [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] of 0.5 |
The goal of this task is to generate a 1000-element array (vector, list, whatever it's called in your language) filled with normally distributed random numbers with a mean of 1.0 and a [http://en.wikipedia.org/wiki/Standard_deviation standard deviation] of 0.5 |
||
{{header|Ada}} |
|||
==[[Ada]]== |
|||
[[Category:Ada]] |
|||
with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random; |
with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random; |
||
with Ada.Numerics.Generic_Elementary_Functions; |
with Ada.Numerics.Generic_Elementary_Functions; |
||
Line 35: | Line 34: | ||
end Normal_Random; |
end Normal_Random; |
||
{{header|C plus plus|C++}} |
|||
[[Category:C plus plus]] |
|||
#include <cstdlib> // for rand |
#include <cstdlib> // for rand |
||
#include <cmath> // for atan, sqrt, log, cos |
#include <cmath> // for atan, sqrt, log, cos |
||
Line 64: | Line 62: | ||
} |
} |
||
{{header|E}} |
|||
==[[E]]== |
|||
[[Category:E]] |
|||
accum [] for _ in 1..1000 { _.with(entropy.nextGaussian()) } |
accum [] for _ in 1..1000 { _.with(entropy.nextGaussian()) } |
||
{{header|IDL}} |
|||
==[[IDL]]== |
|||
[[Category:IDL]] |
|||
result = 1.0 + 0.5*randomn(seed,1000) |
result = 1.0 + 0.5*randomn(seed,1000) |
||
{{header|Java}} |
|||
[[Category:Java]] |
|||
double[] list = new double[1000]; |
double[] list = new double[1000]; |
||
Random rng = new Random(); |
Random rng = new Random(); |
||
Line 82: | Line 77: | ||
} |
} |
||
{{header|MAXScript}} |
|||
[[Category:MAXScript]] |
|||
randList = #() |
randList = #() |
||
for i in 1 to 1000 do append randList (random 0.0 2.0) |
for i in 1 to 1000 do append randList (random 0.0 2.0) |
||
{{header|Perl}} |
|||
[[Category:Perl]] |
|||
map {.5 + rand} 1..1000 |
map {.5 + rand} 1..1000 |
||
{{header|Pop11}} |
|||
[[Category:Pop11]] |
|||
;;; Choose radians as arguments to trigonometic functions |
;;; Choose radians as arguments to trigonometic functions |
||
Line 110: | Line 102: | ||
consvector(1000) -> array; |
consvector(1000) -> array; |
||
{{header|Python}} |
|||
[[Category:Python]] |
|||
'''Interpreter:''' [[Python]] 2.5 |
'''Interpreter:''' [[Python]] 2.5 |
||
Line 117: | Line 108: | ||
randList = [random.gauss(1, .5) for i in range(1000)] |
randList = [random.gauss(1, .5) for i in range(1000)] |
||
{{header|Tcl}} |
|||
==[[Tcl]]== |
|||
[[Category:Tcl]] |
|||
proc nrand {} {return [expr sqrt(-2*log(rand()))*cos(4*acos(0)*rand())]} |
proc nrand {} {return [expr sqrt(-2*log(rand()))*cos(4*acos(0)*rand())]} |
Revision as of 21:44, 17 September 2007
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
The goal of this task is to generate a 1000-element array (vector, list, whatever it's called in your language) filled with normally distributed random numbers with a mean of 1.0 and a standard deviation of 0.5
with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random; with Ada.Numerics.Generic_Elementary_Functions; procedure Normal_Random is Seed : Generator; function Normal_Distribution(Seed : Generator) return Float is package Elementary_Flt is new Ada.Numerics.Generic_Elementary_Functions(Float); use Elementary_Flt; use Ada.Numerics; R1 : Float; R2 : Float; Mu : constant Float := 1.0; Sigma : constant Float := 0.5; begin R1 := Random(Seed); R2 := Random(Seed); return Mu + (Sigma * Sqrt(-2.0 * Log(X => R1, Base => 10.0)) * Cos(2.0 * Pi * R2)); end Normal_Distribution; type Normal_Array is array(1..1000) of Float; Distribution : Normal_Array; begin Reset(Seed); for I in Distribution'range loop Distribution(I) := Normal_Distribution(Seed); end loop; end Normal_Random;
#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)); }
accum [] for _ in 1..1000 { _.with(entropy.nextGaussian()) }
result = 1.0 + 0.5*randomn(seed,1000)
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() }
randList = #() for i in 1 to 1000 do append randList (random 0.0 2.0)
map {.5 + rand} 1..1000
;;; Choose radians as arguments to trigonometic functions true -> popradians;
;;; procedure generating standard normal distribution define random_normal() -> result; lvars r1 = random0(1.0), r2 = random0(1.0); cos(2*pi*r1)*sqrt(-2*log(r2)) -> result enddefine;
lvars array, i;
;;; Put numbers on the stack for i from 1 to 1000 do 1.0+0.5*random_normal() endfor; ;;; collect them into array consvector(1000) -> array;
Python Interpreter: Python 2.5
import random randList = [random.gauss(1, .5) for i in range(1000)]
proc nrand {} {return [expr sqrt(-2*log(rand()))*cos(4*acos(0)*rand())]} for {set i 0} {$i < 1000} {incr i} {lappend result [expr 1+.5*nrand()]}