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: 11001010011100001111010100100110The 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
| Bits | Name | Values |
|---|---|---|
| 4 | Nibble (one hex digit) | 16 |
| 8 | Byte / octet | 256 |
| 16 | Word (short) | 65,536 |
| 32 | Double word / int, IPv4 address | about 4.3 billion |
| 64 | Long / pointer size on 64-bit systems | about 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
0bfor 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
11101101are 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.