Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,35 @@
PROGRAM DISTRIBUTION
!
! for rosettacode.org
!
! formulas taken from TI-59 Master Library manual
CONST NUM_ITEM=1000
!VAR SUMX#,SUMX2#,R1#,R2#,Z#,I%
DIM A#[1000]
BEGIN
! seeds random number generator with system time
RANDOMIZE(TIMER)
PRINT(CHR$(12);) !CLS
SUMX#=0 SUMX2#=0
FOR I%=1 TO NUM_ITEM DO
R1#=RND(1) R2#=RND(1)
Z#=SQR(-2*LOG(R1#))*COS(2*π*R2#)
A#[I%]=Z#/2+1 ! I want a normal distribution with
! mean=1 and std.dev=0.5
SUMX#+=A#[I%] SUMX2#+=A#[I%]*A#[I%]
END FOR
Z#=SUMX#/NUM_ITEM
PRINT("Average is";Z#)
PRINT("Standard dev. is";SQR(SUMX2#/NUM_ITEM-Z#*Z#))
END PROGRAM

View file

@ -0,0 +1 @@
function randg(mean,stddev: float): float;

View file

@ -0,0 +1,36 @@
' FB 1.05.0 Win64
Const pi As Double = 3.141592653589793
Randomize
' Generates normally distributed random numbers with mean 0 and standard deviation 1
Function randomNormal() As Double
Return Cos(2.0 * pi * Rnd) * Sqr(-2.0 * Log(Rnd))
End Function
Dim r(0 To 999) As Double
Dim sum As Double = 0.0
' Generate 1000 normally distributed random numbers
' with mean 1 and standard deviation 0.5
' and calculate their sum
For i As Integer = 0 To 999
r(i) = 1.0 + randomNormal/2.0
sum += r(i)
Next
Dim mean As Double = sum / 1000.0
Dim sd As Double
sum = 0.0
' Now calculate their standard deviation
For i As Integer = 0 To 999
sum += (r(i) - mean) ^ 2.0
Next
sd = Sqr(sum/1000.0)
Print "Mean is "; mean
Print "Standard Deviation is"; sd
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,31 @@
include "ConsoleWindow"
local fn RandomZeroToOne as double
dim as double result
BeginCCode
result = (double)( (rand() % 100000 ) * 0.00001 );
EndC
end fn = result
local fn RandomGaussian as double
dim as double r
r = fn RandomZeroToOne
end fn = 1 + .5 * ( sqr( -2 * log(r) ) * cos( 2 * pi * r ) )
dim as long i
dim as double mean, std, a(1000)
for i = 1 to 1000
a(i) = fn RandomGaussian
mean += a(i)
next
mean = mean / 1000
for i = 1 to 1000
std += ( a(i) - mean )^2
next
std = std / 1000
print " Average:"; mean
print "Standard Deviation:"; std

View file

@ -0,0 +1,14 @@
import math, strutils
const precisn = 5
var rs: TRunningStat
proc normGauss: float {.inline.} = 1 + 0.76 * cos(2*PI*random(1.0)) * sqrt(-2*log10(random(1.0)))
randomize()
for j in 0..5:
for i in 0..1000:
rs.push(normGauss())
echo("mean: ", $formatFloat(rs.mean,ffDecimal,precisn),
" stdDev: ", $formatFloat(rs.standardDeviation(),ffDecimal,precisn))

View file

@ -0,0 +1,8 @@
function RandomNormal()
return sqrt(-2*log(rnd())) * cos(2*PI*rnd())
end function
sequence s = repeat(0,1000)
for i=1 to length(s) do
s[i] = 1 + 0.5 * RandomNormal()
end for

View file

@ -0,0 +1,3 @@
for i = 1 to 10
see random(i) + nl
next i

View file

@ -0,0 +1,2 @@
var arr = 1000.of { 1 + (0.5 * (-2 * 1.rand.log -> sqrt) * (Number.tau * 1.rand -> cos)) }
arr.each { .say }

View file

@ -0,0 +1,26 @@
LOCAL i As Integer, m As Double, n As Integer, sd As Double
py = PI()
SET TALK OFF
SET DECIMALS TO 6
CREATE CURSOR gdev (deviate B(6))
RAND(-1)
n = 1000
m = 1
sd = 0.5
CLEAR
FOR i = 1 TO n
INSERT INTO gdev VALUES (GaussDev(m, 1/sd))
ENDFOR
CALCULATE AVG(deviate), STD(deviate) TO m, sd
? "Mean", m, "Std Dev", sd
SET TALK ON
SET DECIMALS TO
FUNCTION GaussDev(mean As Double, sdev As Double) As Double
LOCAL z As Double
z = SQRT(-2*LOG(RAND()))*COS(py*RAND())
IF sdev # 0
z = mean + z/sdev
ENDIF
RETURN z
ENDFUNC

View file

@ -0,0 +1,8 @@
# 15-bit integers generated using the same formula as rand() from the Microsoft C Runtime.
# The random numbers are in [0 -- 32767] inclusive.
# Input: an array of length at least 2 interpreted as [count, state, ...]
# Output: [count+1, newstate, r] where r is the next pseudo-random number.
def next_rand_Microsoft:
.[0] as $count | .[1] as $state
| ( (214013 * $state) + 2531011) % 2147483648 # mod 2^31
| [$count+1 , ., (. / 65536 | floor) ] ;

View file

@ -0,0 +1,20 @@
# Generate a single number following the normal distribution with mean 0, variance 1,
# using the Box-Muller method: X = sqrt(-2 ln U) * cos(2 pi V) where U and V are uniform on [0,1].
# Input: [n, state]
# Output [n+1, nextstate, r]
def next_rand_normal:
def u: next_rand_Microsoft | .[2] /= 32767;
u as $u1
| ($u1 | u) as $u2
| ((( (8*(1|atan)) * $u1[2]) | cos)
* ((-2 * (($u2[2]) | log)) | sqrt)) as $r
| [ (.[0]+1), $u2[1], $r] ;
# Generate "count" arrays, each containing a random normal variate with the given mean and standard deviation.
# Input: [count, state]
# Output: [updatedcount, updatedstate, rnv]
# where "state" is a seed and "updatedstate" can be used as a seed.
def random_normal_variate(mean; sd; count):
next_rand_normal
| recurse( if .[0] < count then next_rand_normal else empty end)
| .[2] = (.[2] * sd) + mean;

View file

@ -0,0 +1,6 @@
def summary:
length as $l | add as $sum | ($sum/$l) as $a
| reduce .[] as $x (0; . + ( ($x - $a) | .*. ))
| [ $a, (./$l | sqrt)] ;
[ [0,1] | random_normal_variate(1; 0.5; 1000) | .[2] ] | summary