Python Forum
Using Dictionary to Test Evenness of Distribution Generated by Randint Function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Dictionary to Test Evenness of Distribution Generated by Randint Function
#6
One of the very few uses of counter that make it almost worth the disk space it occupies. It was brand new for Python 2.7

I would use dict.get(key, default) which is a safe way to retrieving a value from a dictionary. If the key is not found .get() returns the default value instead of raising a key error. Sorry, but I cannot make myself write this in Python 2.7.
from random import randint
 
frequency = {}
for i in range(1000):
    num = randint(1, 10)
    frequency[num] += frequency.get(num, 0)
 
print(frequency)
Reply


Messages In This Thread
RE: Using Dictionary to Test Evenness of Distribution Generated by Randint Function - by deanhystad - Feb-21-2021, 06:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  distribution fit Glaucio 1 266 Apr-07-2024, 12:30 AM
Last Post: Larz60+
  Weight Distribution 11drk9 11 783 Mar-13-2024, 06:08 AM
Last Post: Pedroski55
  random numbers, randint janeik 2 596 Nov-27-2023, 05:17 PM
Last Post: janeik
  Unexpected output while using random.randint with def terickson2367 1 551 Oct-24-2023, 05:56 AM
Last Post: buran
Information Best distribution method inovermyhead100 0 589 Jul-19-2023, 07:39 AM
Last Post: inovermyhead100
  HOW TO USE C# GENERATED DLL davide_vergnani 2 1,701 Jun-12-2023, 03:35 PM
Last Post: davide_vergnani
  passing dictionary to the function mark588 2 1,025 Dec-19-2022, 07:28 PM
Last Post: deanhystad
  How do I use a whl puython distribution? barryjo 6 1,841 Aug-15-2022, 03:00 AM
Last Post: barryjo
Sad Iterate randint() multiple times when calling a function Jake123 2 2,109 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,207 Jul-03-2021, 10:07 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020