{"id":8816,"date":"2023-11-04T11:11:39","date_gmt":"2023-11-04T05:41:39","guid":{"rendered":"https:\/\/python-programs.com\/?p=8816"},"modified":"2023-11-10T12:16:22","modified_gmt":"2023-11-10T06:46:22","slug":"python-program-to-generate-random-numbers-from-1-to-20-and-append-them-to-the-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-generate-random-numbers-from-1-to-20-and-append-them-to-the-list\/","title":{"rendered":"Python Program to Generate Random Numbers from 1 to 20 and Append Them to the List"},"content":{"rendered":"
Lists in Python:<\/strong><\/p>\n Python\u2019s built-in container types are List and Tuple. Objects of both classes can store various additional objects that can be accessed via index. Lists and tuples, like strings, are sequence data types. Objects of different types can be stored in a list or a tuple.<\/p>\n A list is an ordered collection of objects (of the same or distinct types) separated by commas and surrounded by square brackets.<\/p>\n Random Numbers in Python:<\/strong><\/p>\n The random<\/strong> module in Python defines a set of functions for generating and manipulating random integers. Random()<\/strong>, a pseudo-random number generator function that generates a random float number between 0.0 and 1.0, is used by functions in the random module. These functions are used in a variety of games, lotteries, and other applications that need the creation of random numbers.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n given number of elements =30<\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n We will create a program that generates random numbers from 1 to 20 and adds them to a empty list using append() function.<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n Python has a random module that can create random integers. We utilized the random function in combination with the randint function to produce random numbers.<\/p>\n Syntax:<\/strong><\/p>\n randint takes two inputs: a starting and an ending point. Both must be integers, with the first number always being less than the second.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Lists in Python: Python\u2019s built-in container types are List and Tuple. Objects of both classes can store various additional objects that can be accessed via index. Lists and tuples, like strings, are sequence data types. Objects of different types can be stored in a list or a tuple. A list is an ordered collection of …<\/p>\nThe random number list which contains 30 elements = [6, 18, 11, 5, 16, 12, 6, 20, 12, 1, 5, 20, 20, 18, 13, 12, 5, 7, \r\n14, 9, 4, 5, 13, 18, 19, 19, 19, 20, 12, 2]<\/pre>\n
Enter the total number of elements in the given list =25<\/pre>\n
The random number list which contains 25 elements = [1, 6, 18, 5, 3, 2, 7, 5, 6, 6, 1, 2, 4, 17, 1, 20, 6, 2, 6, 19, 6, \r\n10, 19, 20, 3]<\/pre>\n
Program to Generate Random Numbers from 1 to 20 and Append Them to the List<\/h2>\n
\n
1)randint function<\/h3>\n
randint(start, end)<\/pre>\n
2)Generating n random numbers using randint function(Static Input)<\/h3>\n
\n
# importing random module\r\nimport random\r\n# Take a empty list which stores the random numbers.\r\nrandomlist = []\r\n# Using static input, give\u00a0the number of elements in the list.\r\nterms = 30\r\n# Loop from 1 to number of elements using for loop\r\nfor k in range(terms):\r\n # Generate the random number by passing parameters 1 and 20 to\r\n # randint function and store it in a variable.\r\n randNum = random.randint(1, 20)\r\n # Append this random number variable to the list using append() function\r\n randomlist.append(randNum)\r\n# print the random number list\r\nprint('The random number list which contains',\r\n terms, 'elements = ', randomlist)\r\n<\/pre>\n
The random number list which contains 30 elements = [6, 18, 11, 5, 16, 12, 6, 20, 12, 1, 5, 20, 20, 18, 13, 12, 5, 7, \r\n14, 9, 4, 5, 13, 18, 19, 19, 19, 20, 12, 2]<\/pre>\n
\n
3)Generating n random numbers using randint function(User Input)<\/h3>\n
\n
# importing random module\r\nimport random\r\n# Take a empty list which stores the random numbers.\r\nrandomlist = []\r\n# Scan the number of elements using int(input()).\r\nterms = int(input('Enter the total number of elements in the given list ='))\r\n# Loop from 1 to number of elements using for loop\r\nfor k in range(terms):\r\n # Generate the random number by passing parameters 1 and 20 to\r\n # randint function and store it in a variable.\r\n randNum=random.randint(1, 20)\r\n # Append this random number variable to the list using append() function\r\n randomlist.append(randNum)\r\n# print the random number list\r\nprint('The random number list which contains',\r\n terms, 'elements = ', randomlist)\r\n<\/pre>\n
Enter the total number of elements in the given list =25\r\nThe random number list which contains 25 elements = [1, 6, 18, 5, 3, 2, 7, 5, 6, 6, 1, 2, 4, 17, 1, 20, 6, 2, 6, 19, 6, 10,\r\n 19, 20, 3]<\/pre>\n
\n