Random numbers: Difference between revisions

Random numbers en BASIC256
(→‎{{header|PARI/GP}}: one programming error corrected, visual check of improve of distribution. No further tests.)
(Random numbers en BASIC256)
Line 168:
a(i) = 1 + SQR(-2 * LOG(RND)) * COS(2 * pi * RND)
NEXT i
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<lang BASIC256># Generates normally distributed random numbers with mean 0 and standard deviation 1
function randomNormal()
return cos(2.0 * pi * rand) * sqr(-2.0 * log(rand))
end function
 
dim r(1000)
sum = 0.0
# Generate 1000 normally distributed random numbers
# with mean 1 and standard deviation 0.5
# and calculate their sum
for i = 0 to 999
r[i] = 1.0 + randomNormal() / 2.0
sum += r[i]
next i
 
mean = sum / 1000.0
sum = 0.0
# Now calculate their standard deviation
for i = 0 to 999
sum += (r[i] - mean) ^ 2.0
next i
sd = sqr(sum/1000.0)
 
print "Mean is "; mean
print "Standard Deviation is "; sd
end</lang>
{{out}}
<pre>Mean is 1.002092
Standard Deviation is 0.4838570687</pre>
 
 
==={{header|Commodore BASIC}}===
2,156

edits