rand throw

mail@pastecode.io avatar
unknown
julia
2 years ago
1.8 kB
9
Indexable
using Random, Statistics, BenchmarkTools, CUDA
hit_table = zeros(20,3)
samples = 100000

function hit_table_for(dicen=1,samples=10000)
    hit_table = zeros(20,3)
    for dices in 1:dicen
       for val2hit in 1:20
            for i in 1:samples
                rol = rand(1:20,(1,dices))
                rol_m = maximum(rol)
                (rol_m >= val2hit)&&(@view(hit_table[val2hit,dices]).+=1)
                end
        end
    end
    hit_table
end

@time hit_table_for(3,10000)
hit_table
hit_perc = hit_table/samples

function hit_table_vec(dicen=1,samples=10000)
    value_mat = [f for f in 1:20, s in 1:samples, d in 1:dicen]
    value_mat = reduce(max, value_mat, dims = 3)
    rand_mat = rand(1:20, (20,samples))
    bool_mat = rand_mat.>=value_mat
    hit_vec = mean(bool_mat, dims=2)
    return reshape(hit_vec, (20,1))
end

@time hit_table_vec(1,10000)

function hit_table_mat(samples=10000)
    hit_mat = hcat(hit_table_vec(1,samples),hit_table_vec(2,samples),hit_table_vec(3,samples))
    return hit_mat
end

samples=10000
@time hit_table_mat(samples)

function hit_table_cuvec(dicen=1,samples=10000)
    value_mat = CuArray([f for f in 1:20, s in 1:samples])
    rand_mat = CuArray(rand(1:20, (20,samples,dicen)))
    rand_mat = reduce(max, rand_mat, dims = 3)
    bool_mat = rand_mat.>=value_mat
    hit_vec = mean(bool_mat, dims=2)
    return reshape(hit_vec, (20,1))
end
function hit_table_cumat(samples=10000)
    hit_mat = hcat(hit_table_cuvec(1,samples),hit_table_cuvec(2,samples),hit_table_cuvec(3,samples))
    return hit_mat
end

@time hit_table_cuvec(1,10000)

@benchmark hit_table_for(3,samples)
@benchmark hit_table_mat(samples)
@benchmark hit_table_cumat(samples)