Random numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎[[Java]]: Fixed code)
m (→‎[[Java]]: No need to use (expensive) wrapper class here; fixed typo in assignment statement)
Line 5: Line 5:
==[[Java]]==
==[[Java]]==
[[Category:Java]]
[[Category:Java]]
Double[] list = new Double[1000];
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()
}
}



Revision as of 18:31, 31 May 2007

Task
Random numbers
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

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()
}

IDL

result = 1.0 + 0.5*randomn(seed,1000)


Tcl

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()]}


Python

import random
randList = [random.gauss(1, .5) for i in range(1000)]