FormatterHQ

Random Binary Number Generator

Create random strings of 0s and 1s with the bit length you choose, optionally prefixed with 0b. Ideal for testing parsers, bit masks and teaching binary.

Loading tool…
Ready.

How to use the Random Binary Generator

  1. Set the length in bits (1 to 1024), for example 8 for a byte.
  2. Choose how many binary numbers to create.
  3. Optionally add the 0b prefix and require unique values.
  4. Generate and copy the bit strings.

Random binary generator for bit strings of any length

Binary is base 2: every digit (bit) is either 0 or 1. This random binary generator produces strings of the exact bit length you choose, where each bit is independently 0 or 1 with equal probability. Leading zeros are kept, so an 8-bit value such as 00101101 always has eight digits, which is what you want when the bits represent flags or a fixed-width register.

Examples

8 bits:           10110010
16 bits, 0b:      0b0111001011000101
32 bits:          11001010011100001111010100100110

The 0b prefix is the binary literal syntax in JavaScript, Python, Java, C#, C++14 and Rust, so prefixed output can be pasted straight into source code.

Common bit lengths

BitsNameValues
4Nibble (one hex digit)16
8Byte / octet256
16Word (short)65,536
32Double word / int, IPv4 addressabout 4.3 billion
64Long / pointer size on 64-bit systemsabout 1.8 × 1019

What random binary is good for

  • Teaching – give students random bytes to convert to decimal or hexadecimal and check the answers with binary to decimal.
  • Bitwise exercises – generate two operands and practice AND, OR and XOR with the bitwise calculator.
  • Parser tests – feed long bit strings into code that reads binary literals, checks lengths or packs bits into bytes.
  • Feature flags and masks – create random combinations of flags to test permission logic.

Options

  • Length – 1 to 1024 bits per value.
  • How many – up to 10,000 values.
  • Prefix – add 0b for code literals.
  • Unique – avoid duplicates; with very short lengths there are few possible values (only 16 for 4 bits).
  • Separator – one per line, comma-separated or space-separated.

Reading a random binary number

Each position is worth a power of two, starting from the right: 1, 2, 4, 8, 16 and so on. The 8-bit value 10110010 is therefore 128 + 32 + 16 + 2 = 178 in decimal, or B2 in hex. Working a few random values out by hand is a quick way to get comfortable with binary, and the generator gives you an endless supply of new exercises.

Bits come from the Web Crypto API in your browser, so they are suitable even for security experiments. For raw bytes in hex or Base64 instead of bit strings, see the random byte generator.

What truly random bits look like

People asked to type random 0s and 1s by hand avoid long runs and switch too often. Real random bits are streakier than intuition suggests, which is worth knowing when you judge the output or build exercises from it:

  • Runs are normal. About three out of four random 8-bit values contain three or more equal bits in a row. Only 68 of the 256 possible bytes avoid such a run.
  • Balance is only on average. A random byte has four 1s on average, but just 70 of the 256 bytes have exactly four; values like 11101101 are perfectly ordinary.
  • Extremes are rare but possible. The chance that an n-bit value is all zeros is 1 in 2n: 1 in 256 for a byte, and practically never for 128 bits.

So a batch that “looks too regular” or “too streaky” is not a sign of a problem. The bits come from the browser's cryptographic random source and are independent of one another.

Frequently asked questions

Are leading zeros included?
Yes. Each value has exactly the number of bits you specify, so 8-bit values such as 00010011 keep their leading zeros.
How can I convert the binary numbers to decimal?
Paste them into the binary to decimal converter on this site, or in code use parseInt(value, 2) in JavaScript or int(value, 2) in Python.
What does the 0b prefix mean?
It marks a number as a binary literal in languages such as JavaScript, Python, Java, C#, C++ and Rust, so 0b1010 means ten.
Are 0s and 1s equally likely?
Yes. Each bit is chosen independently with a 50% chance of being 0 or 1, using a cryptographically secure random source.

Last updated September 26, 2026

Related tools