{"id":16581,"date":"2021-08-12T09:25:08","date_gmt":"2021-08-12T03:55:08","guid":{"rendered":"https:\/\/python-programs.com\/?p=16581"},"modified":"2021-11-22T18:38:33","modified_gmt":"2021-11-22T13:08:33","slug":"python-program-to-select-a-random-element-from-a-tuple","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-select-a-random-element-from-a-tuple\/","title":{"rendered":"Python Program to Select a Random Element from a Tuple"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Get n Random Items from a List<\/a> As this Random module is one of Python’s predefined modules, its methods return random values.<\/p>\n It selects integers uniformly from a range. For sequences, it has a function to generate a random permutation of a list in-place, as well as a function to generate a random sampling without replacement. Let’s take a look at how to import the Random Module.<\/p>\n The random module in Python is made up of various built-in Methods.<\/p>\n choice():<\/strong>\u00a0 choice() is used to select an item at random from a list, tuple, or other collection.<\/p>\n Because the choice() method returns a single element, we will be using\u00a0 it in looping statements. Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to select a random element from a given Tuple.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python Program to Get n Random Items from a List Random Module in python : As this Random module is one of Python’s predefined modules, its methods return random values. It selects integers uniformly from a range. For sequences, it has a function to generate a random permutation …<\/p>\n
\nRandom Module in python :<\/strong><\/p>\n
\nsample(): <\/strong>To meet our needs, we’ll use sample() to select multiple values.<\/p>\nGiven no of random numbers to be generated = 3\r\nGiven tuple = (\"btechgeeks\", 321, \"good morning\", [7, 8, 5, 33], 35.8)<\/pre>\n
Given no of random numbers to be generated = 4\r\nGiven tuple = (255, \"hello\", \"btechgeeks\", 150,\"good morning\", [1, 2, 3, 4], 100, 98)<\/pre>\n
The given 4 Random numbers are :\r\n[1, 2, 3, 4]\r\n255\r\n255\r\n98<\/pre>\n
Program to Select a Random Element from a Tuple<\/h2>\n
\n
Method #1: Using random.choice() Method (Only one element)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the tuple as static input and store it in a variable.\r\ngvn_tupl = (255, \"hello\", \"btechgeeks\", 150,\r\n \"good morning\", [1, 2, 3, 4], 100, 98)\r\n# Apply random.choice() method for the above given tuple and store it in another variable.\r\nreslt = random.choice(gvn_tupl)\r\n# Print the random element from the above given tuple\r\nprint(\"The random element from the above given tuple = \", reslt)\r\n<\/pre>\n
The random element from the above given tuple = btechgeeks<\/pre>\n
Method #2: Using For Loop (N elements)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the number as static input and store it in a variable.\r\nrandm_numbrs = 3\r\n# Give the tuple as static input and store it in another variable.\r\ngvn_tupl = (\"btechgeeks\", 321, \"good morning\", [7, 8, 5, 33], 35.8)\r\nprint(\"The given\", randm_numbrs, \"Random numbers are :\")\r\n# Loop in the given tuple above given 'n' number of times using For loop.\r\nfor itr in range(randm_numbrs):\r\n # Inside the loop, apply random.choice() method for the above given tuple and\r\n # store it in a variable.\r\n reslt = random.choice(gvn_tupl)\r\n # Print the given numbers of random elements to be generated.\r\n print(reslt)\r\n<\/pre>\n
The given 3 Random numbers are :\r\nbtechgeeks\r\n[7, 8, 5, 33]\r\n321<\/pre>\n
\n