Random numbers: Difference between revisions
Content added Content deleted
m (Applesoft BASIC) |
|||
Line 1,081: | Line 1,081: | ||
Note: To generate the random number, rather than using FB's native "rnd" function, this code wraps C code into the RandomZeroToOne function. |
Note: To generate the random number, rather than using FB's native "rnd" function, this code wraps C code into the RandomZeroToOne function. |
||
<lang futurebasic> |
<lang futurebasic> |
||
window 1 |
|||
include "ConsoleWindow" |
|||
local fn RandomZeroToOne as double |
local fn RandomZeroToOne as double |
||
Line 1,093: | Line 1,093: | ||
dim as double r |
dim as double r |
||
r = fn RandomZeroToOne |
r = fn RandomZeroToOne |
||
end fn = 1 + .5 * ( sqr( -2 * log(r) ) * cos( 2 * pi * r ) ) |
end fn = 1 + .5 * ( sqr( -2 * log(r) ) * cos( 2 * pi * r ) ) |
||
long i |
|||
double mean, std, a(1000) |
|||
for i = 1 to 1000 |
for i = 1 to 1000 |
||
a(i) = fn RandomGaussian |
|||
mean += a(i) |
|||
next |
next |
||
mean = mean / 1000 |
mean = mean / 1000 |
||
for i = 1 to 1000 |
for i = 1 to 1000 |
||
std += ( a(i) - mean )^2 |
|||
next |
next |
||
std = std / 1000 |
std = std / 1000 |
||
print " Average:"; mean |
print " Average: "; mean |
||
print "Standard Deviation:"; std |
print "Standard Deviation: "; std |
||
</lang> |
|||
HandleEvents |
|||
Output: |
|||
<pre> |
|||
Average: 1.0258434498 |
|||
Standard Deviation: 0.2771047023 |
|||
</pre> |
</pre> |
||