{"id":13074,"date":"2021-10-01T11:00:47","date_gmt":"2021-10-01T05:30:47","guid":{"rendered":"https:\/\/python-programs.com\/?p=13074"},"modified":"2021-11-22T18:39:28","modified_gmt":"2021-11-22T13:09:28","slug":"random-choice-of-random-module-in-python-with-no-repeat","status":"publish","type":"post","link":"https:\/\/python-programs.com\/random-choice-of-random-module-in-python-with-no-repeat\/","title":{"rendered":"Random Choice of Random Module in Python with no Repeat"},"content":{"rendered":"
Given the upper limit and lower limit, the task is to generate n natural numbers which are not repeating in Python.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to generate n natural numbers which are not repeating in Python.<\/p>\n Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners<\/a> tutorial.<\/p>\n Approach: Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach: Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Given the upper limit and lower limit, the task is to generate n natural numbers which are not repeating in Python. Examples: Example1: Input: Given N=13 Given lower limit range =19 Given upper limit range =45 Output: The random numbers present in the range from 19 to 45 are : 28 40 24 25 20 …<\/p>\nGiven N=13\r\nGiven lower limit range =19\r\nGiven upper limit range =45<\/pre>\n
The random numbers present in the range from 19 to 45 are :\r\n28 40 24 25 20 44 38 29 21 31 43<\/pre>\n
Given N=19\r\nGiven lower limit range =23\r\nGiven upper limit range =41<\/pre>\n
The random numbers present in the range from 23 to 41 are : 26 27 40 38 37 41 30 35 36 23 25<\/pre>\n
Random choice of Random module in Python with no Repeat<\/h2>\n
\n
Method #1: Using For Loop and randint function (Static Input)<\/h3>\n
\n<\/strong><\/p>\n\n
# Import the random module using the import keyword.\r\nimport random\r\n# Give the number n as static input and store it in a variable.\r\nnumbe = 13\r\n# Give the lower limit range and upper limit range as static input\r\n# and store them in two separate variables.\r\nlowerlimitrange = 19\r\nupperlimitrange = 45\r\n# Take an empty list (say rndmnumbs) and initialize it with an empty list\r\n# using [] or list().\r\nrndmnumbs = []\r\n# Loop till n times using For loop.\r\nfor m in range(numbe):\r\n # Generate a random number using randint(lowerlimitrange,upperlimitrange)\r\n # and store it in a variable.\r\n randomnumbe = random.randint(lowerlimitrange, upperlimitrange)\r\n # Check whether the above random number is present in the list or not\r\n # using not in operator.\r\n if randomnumbe not in rndmnumbs:\r\n # If it is not in the list then append the element\r\n # to the rndmnumbs list using the append() function.\r\n rndmnumbs.append(randomnumbe)\r\n\r\n# Print the rndmnumbs\r\nprint('The random numbers present in the range from',\r\n lowerlimitrange, 'to', upperlimitrange, 'are :')\r\nfor q in rndmnumbs:\r\n print(q, end=' ')\r\n<\/pre>\n
The random numbers present in the range from 19 to 45 are :\r\n28 40 24 25 20 44 38 29 21 31 43<\/pre>\n
Method #2: Using For Loop and randint function (User Input)<\/h3>\n
\n<\/strong><\/p>\n\n
# Import the random module using the import keyword.\r\nimport random\r\n# Give the number n as user input using int(input()) and store it in a variable.\r\nnumbe = int(input('Enter some random number = '))\r\n# Give the lower limit range and upper limit range as user input\r\n# using map(),int(),split(),input() functions.\r\n# Store them in two separate variables.\r\nlowerlimitrange = int(input('Enter random lower limit range = '))\r\nupperlimitrange = int(input('Enter random upper limit range = '))\r\n# Take an empty list (say rndmnumbs) and initialize it with an empty list\r\n# using [] or list().\r\nrndmnumbs = []\r\n# Loop till n times using For loop.\r\nfor m in range(numbe):\r\n # Generate a random number using randint(lowerlimitrange,upperlimitrange)\r\n # and store it in a variable.\r\n randomnumbe = random.randint(lowerlimitrange, upperlimitrange)\r\n # Check whether the above random number is present in the list or not\r\n # using not in operator.\r\n if randomnumbe not in rndmnumbs:\r\n # If it is not in the list then append the element\r\n # to the rndmnumbs list using the append() function.\r\n rndmnumbs.append(randomnumbe)\r\n\r\n# Print the rndmnumbs\r\nprint('The random numbers present in the range from',\r\n lowerlimitrange, 'to', upperlimitrange, 'are :')\r\nfor q in rndmnumbs:\r\n print(q, end=' ')\r\n<\/pre>\n
Enter some random number = 19\r\nEnter random lower limit range = 23\r\nEnter random upper limit range = 41\r\nThe random numbers present in the range from 23 to 41 are :\r\n26 27 40 38 37 41 30 35 36 23 25<\/pre>\n
\n