Random numbers: Difference between revisions
→{{header|BASIC}}: Added ANSI BASIC.
(→{{header|Chipmunk Basic}}: Added.) |
(→{{header|BASIC}}: Added ANSI BASIC.) |
||
(14 intermediate revisions by 9 users not shown) | |||
Line 158:
=={{header|BASIC}}==
==={{header|ANSI BASIC}}===
{{trans|FreeBASIC}}
{{works with|Decimal BASIC}}
<syntaxhighlight lang="basic">
100 REM Random numbers
110 RANDOMIZE
120 DEF
130 DIM R(0 TO 999)
140 LET Sum = 0
150 FOR I = 0 TO 999
160 LET R(I) = 1 + RandomNormal / 2
170 LET Sum = Sum + R(I)
180 NEXT I
190 LET Mean = Sum / 1000
200 LET Sum = 0
210 FOR I = 0 TO 999
220 LET Sum = Sum + (R(I) - Mean) ^ 2
230 NEXT I
240 LET SD = SQR(Sum / 1000)
250 PRINT "Mean is "; Mean
260 PRINT "Standard Deviation is"; SD
270 PRINT
280 END
</syntaxhighlight>
{{out}} Two runs.
<pre>
Mean is 1.00216454061435
Standard Deviation is .504515904812839
</pre>
<pre>
Mean is .995781408878628
Standard Deviation is .499307289407576
</pre>
==={{header|Applesoft BASIC}}===
Line 253 ⟶ 278:
Mean is 0.9781
Standard Deviation is 0.508147
</pre>
==={{header|Commodore BASIC}}===
Line 372 ⟶ 397:
Standard Deviation: 0.2897370762627166
</pre>
==={{header|GW-BASIC}}===
The [[Random_numbers#Commodore_BASIC|Commodore BASIC]] code works in GW-BASIC.
==={{header|Liberty BASIC}}===
Line 380 ⟶ 408:
a( i) =mean +sd *( sqr( -2 * log( rnd( 0))) * cos( 2 * pi * rnd( 0)))
next i</syntaxhighlight>
==={{header|Minimal BASIC}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic">
10 REM Random numbers
20 LET P = 4*ATN(1)
30 RANDOMIZE
40 DEF FNN = COS(2*P*RND)*SQR(-2*LOG(RND))
50 DIM R(999)
60 LET S = 0
70 FOR I = 0 TO 999
80 LET R(I) = 1+FNN/2
90 LET S = S+R(I)
100 NEXT I
110 LET M = S/1000
120 LET S = 0
130 FOR I = 0 TO 999
140 LET S = S+(R(I)-M)^2
150 NEXT I
160 LET D = SQR(S/1000)
170 PRINT "Mean is "; M
180 PRINT "Standard Deviation is"; D
190 PRINT
200 END
</syntaxhighlight>
==={{header|PureBasic}}===
Line 399 ⟶ 452:
a(i) = 1 + 0.5 * RandomNormal()
Next</syntaxhighlight>
==={{header|QuickBASIC}}===
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">
RANDOMIZE TIMER 'seeds random number generator with the system time
pi = 3.141592653589793#
DIM a(1 TO 1000) AS DOUBLE
CLS
FOR i = 1 TO 1000
a(i) = 1 + SQR(-2 * LOG(RND)) * COS(2 * pi * RND)
NEXT i
</syntaxhighlight>
==={{header|Run BASIC}}===
Line 757 ⟶ 822:
<syntaxhighlight lang="text">
numfmt 5 0
e = 2.7182818284590452354
for i = 1 to 1000
a[] &= 1 + 0.5 * sqrt (-2 *
.
avg += v / len a[]
.
print "Average: " & avg
for v in a[]
s += pow (v - avg) 2
.
s = sqrt (s / len a[])
print "Std deviation: " & s
</syntaxhighlight>
Line 870 ⟶ 946:
=={{header|Elena}}==
{{trans|C#}}
ELENA
<syntaxhighlight lang="elena">import extensions;
import extensions'math;
Line 885 ⟶ 961:
real tAvg := 0;
for (int x := 0
{
a[x] := (randomNormal()) / 2 + 1;
Line 895 ⟶ 971:
real s := 0;
for (int x := 0
{
s += power(a[x] - tAvg, 2)
Line 2,404 ⟶ 2,480:
next i
</syntaxhighlight>
=={{header|RPL}}==
≪ RAND LN NEG 2 * √
RAND 2 * π * COS *
→NUM 2 / 1 +
≫ '<span style="color:blue>RANDN</span>' STO
≪ CL∑
1 1000 '''START''' <span style="color:blue>RANDN</span> ∑+ '''NEXT'''
MEAN PSDEV
≫ '<span style="color:blue>TASK</span>' STO
{{out}}
<pre>
1: .990779804949
2: .487204045227
</pre>
The collection is stored in a predefined array named <code>∑DAT</code>, which is automatically created/updated when using the <code>∑+</code> instruction and remains available until the user decides to purge it, typically by calling the <code>CL∑</code> command.
=={{header|Ruby}}==
Line 2,620 ⟶ 2,713:
Other implementations of Standard ML have their own random number generators. For example, Moscow ML has a <code>Random</code> structure that is different from the one from SML/NJ.
{{works with|
The SML Basis Library does not provide a routine for uniform deviate generation, and PolyML does not have one. Using a routine from "Monte Carlo" by Fishman (Springer), in the function uniformdeviate, and avoiding the slow IntInf's:
<syntaxhighlight lang="
val urandomlist = fn seed => fn n =>
let
Line 2,744 ⟶ 2,837:
RETURN z
ENDFUNC
</syntaxhighlight>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Vlang">
import crypto.rand
fn main() {
mut nums := []u64{}
for _ in 0..1000 {
nums << rand.int_u64(10000) or {0} // returns random unsigned 64-bit integer from real OS source of entropy
}
println(nums)
}
</syntaxhighlight>
=={{header|Wren}}==
<syntaxhighlight lang="
var rand = Random.new()
Line 2,777 ⟶ 2,883:
Actual std dev: 0.4961645117026
</pre>
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">define PI = 3.14159265358979323846;
func real DRand; \Uniform distribution, [0..1]
return float(Ran(1_000_000)) / 1e6;
func real RandomNormal; \Normal distribution, centered on 0, std dev 1
return sqrt(-2.*Log(DRand)) * Cos(2.*PI*DRand);
int I;
real Rands(1000);
for I:= 0 to 1000-1 do
Rands(I):= 1.0 + 0.5*RandomNormal</syntaxhighlight>
=={{header|Yorick}}==
|