Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Using a Dictionary to Test the Evenness of the Distribution Generated by the Randint Function

Hi,

This is my first post on any sort of coding website and I'm an absolute beginner at programming. I'm trying to learn Python and am currently about halfway through an introductory book in which the author presents a block of code showing how to test the "evenness" of the randint function in Python using a dictionary (taking the range of numbers from 1 to 10 as an example). I'm having trouble figuring out exactly what is going on in this code. Here it is:

Python:
frequency = {}
for i in range(1000):
    num = randint(1, 10)
    if frequency.has_key(num):
        frequency[num] = frequency[num] + 1
        else:
            frequency[num] = 1
           
print frequency

The output from this code is given in the book as follows:
{1: 99, 2: 105, 3: 93, 4: 114, 5: 81, 6: 95, 7: 82, 8: 116, 9: 118, 10: 97}

The author writes that "It's not perfectly even, but most statisticians would be happy with our result."

Here is what I think the code is doing:

The first line defines the "frequency" variable as an empty dictionary.
The second and third lines generate a random number between 1 and 10 1,000 times (I think).
The lines after this with the "if ... else" statement have me a bit stumped. It seems to me that the "if frequency.has_key(num)" criterion will always be met, so the resulting frequency of each number will always have 1 added to it. Why? Does this have something to do with the fact that the range function in Python increments by 1 by default? I'm having trouble understanding how the "if ... else" statement helps generate a frequency distribution for numbers 1 to 10.

Also, if I'm correct that the "if frequency.has_key(num)" criterion will always be met, is the "else" statement at the end of the block of code even necessary?

Thanks in advance for any help.
 
Last edited:

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom