Created
August 12, 2018 07:50
-
-
Save SeanSyue/7b4c352d43648cd2c964ae30ca13cae0 to your computer and use it in GitHub Desktop.
Example on how to generate binary numbers in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
from bitstring import BitArray | |
# `bin()` function explained | |
bin30 = bin(30) | |
print("--> bin30:\n", bin30) | |
print("--> type(bin30):\n", type(bin30)) | |
print("--> int(bin30, 2):\n", int(bin30, 2)) | |
# generate a long list of random binary numbers | |
a = [bin(randint(0, 5000)) for _ in range(0, 10)] | |
print("--> a:\n", a) | |
print("--> type(a):\n", type(a)) | |
# ================================================ | |
# Alternative: use `BitArray` module (pip install bitstring). | |
b = BitArray([randint(0, 1) for _ in range(50)]) | |
print("--> b:", b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment