Ethan Hall
Ethan Hall

Reputation: 5

Issue with function that calculates probability

I have a function that takes an input, being the number of people, and calculates the probability that those people will have the same randomly generated birthday. I am using a dictionary and a randomly generated array, and then looping that 5000 times. However there seems to be an issue as when I try to run the function I get an error: ArgumentError: Dict(kv): kv needs to be an iterator of tuples or pairs. How can I fix this? Thank you!!

function prob_same_bday(numpeople::Int64)
    samebday = 0
    for i = 1:5000

        arr1 = rand(1:365, 1, numpeople)

        d = Dict(arr1)
        for val in itr
            d[val] = get!(d, val, 0) + 1
            if get!(d, val, 0) > 1
                samebday = samebday + 1
            else
                continue
            i = i + 1
            end
        end
    end
    return samebday / 5000
end

Upvotes: 0

Views: 50

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

You need just a 1-dimensional arryay so it should be

arr1 = rand(1:365, numpeople)

Now I assume you want to store number of hits in a dictionary. You cannot transfer array to dictionary like this. You could for example do:

d = Dict(1:numpeople .=> 0)  

Note that d would work as good as a Vector

Looks like a homework assignment so keep trying :=)

Upvotes: 1

Related Questions