Reputation: 99
I want to print or plot list of binary numbers which are randomly generated. I have print and plot random numbers between 1 and 5000 and my code is as under;
for a in range(0, 5000): a=random.sample(range(0, 5000), 5000) print (a) plt.plot(a) plt.show()
This code is running perfectly. but I want Binary numbers instead of Decimal numbers. kindly help me in this regard.
Upvotes: 3
Views: 22368
Reputation: 34257
To get the binary representation of your random generated number (decimal int) , use bin()
For instance, the following code will print 10 random numbers. in decimal and binary
import random
for i in range(0, 10):
a = random.randint(0, 5000)
print a, bin(a)
output:
1465 0b10110111001
1624 0b11001011000
2963 0b101110010011
510 0b111111110
3653 0b111001000101
3671 0b111001010111
2624 0b101001000000
4412 0b1000100111100
3910 0b111101000110
2582 0b101000010110
NOTE: in your example i saw some usage in matplotlib however you weren't explicitly asking about matplotlib so i answered more generally
Upvotes: 9