Python libraries: Difference between revisions

From GotOpinion
Jump to navigation Jump to search
m added getrandbits modoule examples
Line 1: Line 1:
== [https://docs.python.org/3/library/random.html Random] module ==
=== Generate random integer with random number of bits ===
Examples using IDLE with Python 3.7.2 and <code>>>> from random import getrandbits</code>
First I simply return a random integer using 4 as an argument, which produces random integers from 0 to 15 or '0000' to '1111' in binary, , also known as a [https://en.wikipedia.org/wiki/Nibble nibble], or '0' to 'f' in hexadecimal (represented as 0x0 to 0xf)
<pre>>>> getrandbits(4)
0
>>> getrandbits(4)
15</pre>
Cast the integer into binary using 4, 8, 16, 32, 64, and 128 bits
<pre>>>> bin(getrandbits(4))
'0b101'
>>> bin(getrandbits(4))
'0b1010'
>>> bin(getrandbits(4))
'0b1001'
>>> bin(getrandbits(8))
'0b10100110'
>>> bin(getrandbits(8))
'0b1001000'
>>> bin(getrandbits(16))
'0b11001110111101'
>>> bin(getrandbits(32))
'0b10111001011000110110000111011001'
>>> bin(getrandbits(64))
'0b1001111101111000100100110111000101011000010010101001001011100010'
>>> bin(getrandbits(128))
'0b10100111110000000100110000001110001111111101110010110110111111010111111110010000011000111011110000011100101011111001101001010101'</pre>
Basic output can be boring. Let us do some formatting with 4, 8, 16, 32, 64, and 128 bits
<pre>>>> '{0:08b}'.format(getrandbits(4))
'00001110'
>>> '{0:08b}'.format(getrandbits(8))
'10000010'
>>> '{0:08b}'.format(getrandbits(8))
'01101100'
>>> '{0:08b}'.format(getrandbits(8))
'00101011'
>>> '0x{0:01x}'.format(getrandbits(4))
'0xc'
>>> '0x{0:01x}'.format(getrandbits(4))
'0x6'
>>> '0x{0:01x}'.format(getrandbits(8))
'0xf5'
>>> '0x{0:01x}'.format(getrandbits(8))
'0xfc'
>>> '0x{0:08x}'.format(getrandbits(4))
'0x0000000d'
>>> '0x{0:08x}'.format(getrandbits(8))
'0x00000093'
>>> '0x{0:08x}'.format(getrandbits(16))
'0x0000daed'
>>> '0x{0:08x}'.format(getrandbits(32))
'0x90967fce'
>>> '0x{0:08x}'.format(getrandbits(64))
'0x89cc0e8dc42edf3'
>>> '0x{0:08x}'.format(getrandbits(128))
'0xc8c4348f8f44487d0a13c004405c647c'</pre>
Explanation of string format syntax used above:
* {} places a variable into a string
* 0 takes the variable at argument position 0, which is getrandbits() return value
* : adds formatting options for position 0 variable
* 08 formats the output to eight digits zero-padded on the left
* b converts the integer to binary representation
* x converts the integer to hexadecimal representation
See [https://docs.python.org/3/library/string.html#string-formatting Python string formatting] docs for additional information
== Matplotlib ==
== Matplotlib ==



Revision as of 14:31, 30 March 2019

Random module

Generate random integer with random number of bits

Examples using IDLE with Python 3.7.2 and >>> from random import getrandbits

First I simply return a random integer using 4 as an argument, which produces random integers from 0 to 15 or '0000' to '1111' in binary, , also known as a nibble, or '0' to 'f' in hexadecimal (represented as 0x0 to 0xf)

>>> getrandbits(4)
0
>>> getrandbits(4)
15

Cast the integer into binary using 4, 8, 16, 32, 64, and 128 bits

>>> bin(getrandbits(4))
'0b101'
>>> bin(getrandbits(4))
'0b1010'
>>> bin(getrandbits(4))
'0b1001'
>>> bin(getrandbits(8))
'0b10100110'
>>> bin(getrandbits(8))
'0b1001000'
>>> bin(getrandbits(16))
'0b11001110111101'
>>> bin(getrandbits(32))
'0b10111001011000110110000111011001'
>>> bin(getrandbits(64))
'0b1001111101111000100100110111000101011000010010101001001011100010'
>>> bin(getrandbits(128))
'0b10100111110000000100110000001110001111111101110010110110111111010111111110010000011000111011110000011100101011111001101001010101'

Basic output can be boring. Let us do some formatting with 4, 8, 16, 32, 64, and 128 bits

>>> '{0:08b}'.format(getrandbits(4))
'00001110'
>>> '{0:08b}'.format(getrandbits(8))
'10000010'
>>> '{0:08b}'.format(getrandbits(8))
'01101100'
>>> '{0:08b}'.format(getrandbits(8))
'00101011'
>>> '0x{0:01x}'.format(getrandbits(4))
'0xc'
>>> '0x{0:01x}'.format(getrandbits(4))
'0x6'
>>> '0x{0:01x}'.format(getrandbits(8))
'0xf5'
>>> '0x{0:01x}'.format(getrandbits(8))
'0xfc'
>>> '0x{0:08x}'.format(getrandbits(4))
'0x0000000d'
>>> '0x{0:08x}'.format(getrandbits(8))
'0x00000093'
>>> '0x{0:08x}'.format(getrandbits(16))
'0x0000daed'
>>> '0x{0:08x}'.format(getrandbits(32))
'0x90967fce'
>>> '0x{0:08x}'.format(getrandbits(64))
'0x89cc0e8dc42edf3'
>>> '0x{0:08x}'.format(getrandbits(128))
'0xc8c4348f8f44487d0a13c004405c647c'

Explanation of string format syntax used above:

  • {} places a variable into a string
  • 0 takes the variable at argument position 0, which is getrandbits() return value
  • : adds formatting options for position 0 variable
  • 08 formats the output to eight digits zero-padded on the left
  • b converts the integer to binary representation
  • x converts the integer to hexadecimal representation

See Python string formatting docs for additional information


Matplotlib

import matplotlib.pyplot

or

import matplotlib.pyplot as plt

Example: build a histogram

To Python