Articles

Program to generate 100 random numbers from 1 to 10 and find range with highest density in Python

Program to generate 100 random numbers from 1 to 10 and find range with highest density in Python


In this python programing article, we are going to learn

  • program to generate 100 random numbers from 1 to 10 and find range with highest density in python

Program to generate 100 random numbers from 1 to 10 and find range with highest density in Python

The program is as follows:

 

# Owner : TutorialsInhand Author : Devjeet Roy

import random

list_of_numbers = list()
dict_density = dict()

for i in range(100):
    n = random.randint(1,10)
    list_of_numbers.append(n)

for i in list_of_numbers:

    if i >= 1 and i <= 2:
        dict_density['1-to-2'] = dict_density.get('1-to-2', 0) + 1
    
    elif i >= 3 and i <= 4:
        dict_density['3-to-4'] = dict_density.get('3-to-4', 0) + 1
    
    elif i >= 5 and i <= 6:
        dict_density['5-to-6'] = dict_density.get('5-to-6', 0) + 1
    
    elif i >= 7 and i <= 8:
        dict_density['7-to-8'] = dict_density.get('7-to-8', 0) + 1
    
    elif i >= 9 and i <= 10:
        dict_density['9-to-10'] = dict_density.get('9-to-10', 0) + 1
    
    else:
        pass

max_range = None
max_occurrence = 0
for k, v in dict_density.items():
    if v > max_occurrence:
        max_occurrence = v
        max_range = k

print("The maximum range occurred for:",max_range)

The output of the program is as follows:

PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py         
The maximum range occurred for: 7-to-8
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
The maximum range occurred for: 1-to-2                                     
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py     
The maximum range occurred for: 9-to-10                                    
PS C:\Users\DEVJEET\Desktop\tutorialsInHand>

Few important tips about the program

1. We have used the "random" library here to generate 100 random numbers and push them into the list.

2. Then we iterated upon the elements of the list and kept track of the elements and incremented the ranges as per the occurrence of the numbers.

3. Lastly, from the dictionary, we found out the range with the highest occurrence density.

4. The output will be different each time you run and it is randomized.

 

program-to-generate-100-numbers-from-1-to-10-and-determine-highest-density

 


Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Data Science Enthusiast
Page Views :    Published Date : Oct 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!