Rebol3 Code Examplex


Random numbers

Generate a collection filled with 1000 normally distributed random (or pseudo-random) numbers with a mean of 1.0 and a standard deviation of 0.5

Rebol [
    title: "Rosetta code: Random numbers"
    file:  %Random_numbers.r3
    url:   https://rosettacode.org/wiki/Random_numbers
]

box-muller: func [
    {Returns normally distributed random value with given mean and standard deviation}
    mean [number!]
    std  [number!]
][
    mean + (std * (sqrt -2.0 * log-e random 1.0) * cos (2.0 * pi * random 1.0))
]

values: make vector! [float32! 1000]
repeat i 1000 [
    values/:i: box-muller 1.0 0.5
]

print as-yellow "First 5 values:"
print copy/part values 5
print ""
print as-yellow "Statistics:"
print query values object!

Output:

First 5 values:
0.885372877120972 0.845744550228119 1.07981729507446 1.24346947669983 0.263566732406616

Statistics:
signed: #(true)
type: decimal!
size: 32
length: 1000
minimum: -0.662445545196533
maximum: 2.4067714214325
range: 3.06921696662903
sum: 1006.70769778593
mean: 1.00670769778593
median: 1.00160211324692
variance: 244.560819343638
population-deviation: 0.494530908380496
sample-deviation: 0.494778359438453