{"id":25488,"date":"2021-11-16T08:40:04","date_gmt":"2021-11-16T03:10:04","guid":{"rendered":"https:\/\/python-programs.com\/?p=25488"},"modified":"2021-11-16T08:40:04","modified_gmt":"2021-11-16T03:10:04","slug":"python-random-getrandbits-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-random-getrandbits-method-with-examples\/","title":{"rendered":"Python Random getrandbits() Method with Examples"},"content":{"rendered":"
In Python, the random module is used to generate random numbers. This is not truly random; rather, it is used to generate pseudo-random numbers. This implies that these numbers can be determined at random.<\/p>\n
random getrandbits() Method in Python:<\/strong><\/p>\n getrandbits() method returns an integer of the specified size (in bits).<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n num:<\/strong> This is Required. A number indicating the size of the returned integer in bits.<\/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 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 <\/p>\n","protected":false},"excerpt":{"rendered":" In Python, the random module is used to generate random numbers. This is not truly random; rather, it is used to generate pseudo-random numbers. This implies that these numbers can be determined at random. random getrandbits() Method in Python: getrandbits() method returns an integer of the specified size (in bits). Syntax: random.getrandbits(num) Parameters: num: This …<\/p>\nrandom.getrandbits(num)<\/pre>\n
Given number(size) = 6<\/pre>\n
The random integer = 56<\/pre>\n
Given number(size) = 10<\/pre>\n
The random integer = 59<\/pre>\n
Random getrandbits() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the number(size) as static input and store it in a variable.\r\ngvn_numb = 6\r\n# Pass the given number as an argument to the random.getrandbits() method that\r\n# returns an integer of the given size in bits.\r\n# Store it in another variable.\r\nrslt = random.getrandbits(gvn_numb)\r\n# Print an integer with a size of a given number of bits.\r\nprint(\"The random integer = \", rslt)\r\n<\/pre>\n
The random integer = 56<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the number as user input using the int(input()) function and\r\n# store it in a variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n# Pass the given number as an argument to the random.getrandbits() method that\r\n# returns an integer of the given size in bits.\r\n# Store it in another variable.\r\nrslt = random.getrandbits(gvn_numb)\r\n# Print an integer with a size of a given number of bits.\r\nprint(\"The random integer = \", rslt)\r\n<\/pre>\n
Enter some random number = 10\r\nThe random integer = 59<\/pre>\n