2

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;

generation of random numbers

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.

1 Answer 1

8

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

Online demo - https://repl.it/Dx3O

NOTE: in your example i saw some usage in matplotlib however you weren't explicitly asking about matplotlib so i answered more generally

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.