{"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>
\nRandom Module in python :<\/strong><\/p>\n

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.
\nsample(): <\/strong>To meet our needs, we’ll use sample() to select multiple values.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given no of random numbers to be generated = 3\r\nGiven tuple = (\"btechgeeks\", 321, \"good morning\", [7, 8, 5, 33], 35.8)<\/pre>\n

Output:<\/strong><\/p>\n

Example 2:<\/strong><\/p>\n

Input:<\/strong><\/p>\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

Output:<\/strong><\/p>\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

Below are the ways to select a random element from a given Tuple.<\/p>\n