Random Number and Monte Carlo Simulation
using Random
Random.seed!(1974)
println("Seed 1974: ", rand(), "\t", rand(), "\t", rand())
Random.seed!(1975)
println("Seed 1975: ", rand(), "\t", rand(), "\t", rand())
Random.seed!(1974)
println("Seed 1974: ", rand(), "\t", rand(), "\t", rand())
using Plots, Random
totalNumberOfPoints=10^5
Random.seed!(1);
points = [eachcol(rand(2,totalNumberOfPoints))...];
inPoints = findall(x -> x[1]^2 + x[2]^2 <= 1,points)
println("the approximated value of pi is ", 4*length(inPoints)/totalNumberOfPoints)
scatter(first.(points[inPoints]),last.(points[inPoints]),ms=1,msw=0, size=(400,400));
outPoints = findall(x -> x[1]^2 + x[2]^2 > 1,points)
scatter!(first.(points[outPoints]),last.(points[outPoints]),ms=1, msw=0, size=(400,400), legend=:none)
using Random, LinearAlgebra, Plots
Random.seed!()
N = 10^5;
data = [[rand(),rand()] for _ in 1:N];
indata = filter((x) -> (norm(x) <= 1), data);
outdata = filter((x) -> (norm(x) > 1), data);
scatter(first.(indata), last.(indata), c=:blue, ms=1, msw=0)
scatter!(first.(outdata), last.(outdata), c=:red, ms=1, msw=0,
xlims=(0,1), ylims=(0,1), legend=:none, ratio=:equal)
- Inside a Simple Pseudorandom Number Generator
- Linear Congruential Generators:
using Plots, LaTeXStrings, Measures
a, c, m = 69069, 1, 2^32
next(z) = (a*z + c) % m
N = 10^6
data = Array{Float64,1}(undef,N)
x = 808
for i in 1:N
data[i] = x/m
global x = next(x)
end
p1 = scatter(1:1000, data[1:1000],
c=:blue, m=4, msw=0, xlabel=L"n", ylabel=L"x_n")
p2 = histogram(data, bins=50, normed=:true,
ylims=(0,1.1), xlabel="Support", ylabel="Density")
plot(p1,p2, size=(800,400), legend=:none, margin=5mm)