Generate Random Binary Numbers
Generate up to 1,000 random binary numbers by bit length (1 to 256 bits) or between two decimal values. Keep or drop leading zeros, avoid repeats, bias the chance of a 1, group digits for code, and check each result in decimal, signed and hex.
Generated in your browser with crypto.getRandomValues()
One Row, Three Readings: Unsigned, Signed and Hex
A string of bits has no value until you decide how to read it. The generator shows the three readings you are most likely to need. Take the 8-bit result 11010110:
| Reading | Value | How it is worked out |
|---|---|---|
| Unsigned (Decimal column) | 214 | 128 + 64 + 16 + 4 + 2 |
| Signed, two's complement | −42 | Top bit is 1, so subtract 2⁸: 214 − 256 |
| Hexadecimal | D6 | 1101 = D, 0110 = 6, four bits per hex digit |
The signed column only appears when every number has the same width, because two's complement depends on it: 11010110 is −42 as 8 bits but 214 as part of a 16-bit 0000000011010110. In any fixed width, a leading 1 means negative, so 10000000 is −128 (the most negative 8-bit value) and 01111111 is 127. Choosing always start with 1 therefore makes every signed result negative. To convert longer binary strings to hex on their own, use the binary to hex converter.
Worked Example: Check 10110011 by Doubling
A teacher wants ten 8-bit numbers for a starter exercise. They untick Show decimal and hex, copy the binary column onto the board, then turn the answers back on to mark. One of the numbers is 10110011. Instead of writing out place values, a student reads it from the left: double the running total, then add the next bit.
Read left to right: total × 2 + next bit
| Bit | Working | Running total |
|---|---|---|
| 1 | 0 × 2 + 1 | 1 |
| 0 | 1 × 2 + 0 | 2 |
| 1 | 2 × 2 + 1 | 5 |
| 1 | 5 × 2 + 1 | 11 |
| 0 | 11 × 2 + 0 | 22 |
| 0 | 22 × 2 + 0 | 44 |
| 1 | 44 × 2 + 1 | 89 |
| 1 | 89 × 2 + 1 | 179 |
Doubling never needs a table of powers of two, so it works just as well for 12- or 16-bit numbers. It is also how software parses binary text. For a quick start, the All 16 nibbles, shuffled preset gives every 4-bit value from 0000 to 1111 exactly once in random order: a full set of flash cards.
Leading Zeros: Fixed Width or Exactly n Bits
"A random 8-bit number" can mean two different things, and the leading-zeros setting chooses between them.
- Keep (fixed width): every result has 8 digits, and all 256 values from 0 to 255 are equally likely. Half of them start with 0. This is the right choice for bytes, bit masks and register values.
- Always start with 1: the number really needs 8 bits to write, so the range is 128 to 255 (2⁷ to 2⁸ − 1), only 128 possible values. Use it when an exercise says "an 8-digit binary number", since a number doesn't normally start with 0.
- Drop them: the same values as fixed width, written without padding. Results get shorter at random: half have all 8 digits, and 1 in 16 has 4 digits or fewer, because the first four bits were all 0.
Dropping zeros never changes a value: 00001101 and 1101 are both 13. It only changes how the list looks, and whether the signed reading still makes sense.
Bit Lengths and the Values They Hold
n bits give 2ⁿ patterns. Read unsigned, they run from 0 to 2ⁿ − 1; read as two's complement, from −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1.
| Bits | Unsigned range | Signed range | Where this size appears |
|---|---|---|---|
| 1 | 0 to 1 | −1 to 0 | A coin flip, a true/false flag |
| 4 | 0 to 15 | −8 to 7 | A nibble: one hex digit |
| 8 | 0 to 255 | −128 to 127 | A byte; one colour channel in #RRGGBB |
| 16 | 0 to 65,535 | −32,768 to 32,767 | TCP/UDP port numbers; short |
| 32 | 0 to 4,294,967,295 | −2,147,483,648 to 2,147,483,647 | An IPv4 address; int |
| 53 | 0 to 9,007,199,254,740,991 | — | Largest exact integer in a JavaScript number |
| 64 | 0 to 18,446,744,073,709,551,615 | ±9.22 × 10¹⁸ | long / int64 |
| 128 | 0 to about 3.4 × 10³⁸ | — | A UUID; an IPv6 address |
Past 53 bits, the decimal values on this page are still exact because it uses BigInt, but pasting them into JavaScript as plain numbers, or into a spreadsheet, will round them.
A Random Binary Number Between Two Values
Set Size by to Range of values when you need, say, a binary number from 1 to 6 or from 100 to 199, rather than a set number of bits. Results are padded to the width of the largest value, so 1 to 6 gives three-digit results from 001 to 110.
The obvious shortcut, taking a random byte and using the remainder after dividing by the size of the range, is not fair. A byte has 256 values; 256 = 2 × 100 + 56, so for a range of 100 values the remainders 0 to 55 each come from three bytes, while 56 to 99 come from only two. The low values turn up 1.5 times as often.
This page draws just enough bits to cover the range instead (7 bits, 0 to 127, for 100 values) and simply redraws anything too large. With 100 values, 28 of the 128 patterns are rejected, so it takes about 1.28 draws per number on average, and every value in the range is exactly equally likely.
What Random Bits Look Like
The tiles above the results count the ones and find the longest run of identical digits. Both are worth a glance, because genuinely random bits look less tidy than most people expect.
- Balance is close, not exact. In 64 fair bits you expect 32 ones, give or take about 4; about 97% of sets land between 24 and 40. Across 1,000 bits the share of ones is usually between 47% and 53%. A set with exactly half ones every time would be a warning sign.
- Long runs are normal. In 100 random bits the longest run of the same digit averages about 7, and 97% of 100-bit strings contain a run of 5 or more. In 1,000 bits it averages about 10. People inventing "random" sequences by hand rarely write a run longer than 4.
- Repeats are expected in short lengths. Draw 10 four-bit numbers and the chance of at least one repeat is about 97%, since only 16 values exist. Tick No repeats to forbid them.
Chance of a 1 deliberately skews the bits. At 10%, a 64-bit result has about 6 ones on average, which is useful for sparse bit masks, feature-flag test data or simulating a rare event. At 0% or 100% every bit is fixed. Leave it at 50% for ordinary random numbers; the range mode always uses 50%.
Counting ones and runs is the idea behind the first tests in NIST SP 800-22, but passing a glance at two numbers proves nothing on its own. The bits here come from your browser's cryptographically secure generator, crypto.getRandomValues(), and are never sent anywhere. For key material in hex or Base64, use the random bytes generator.
Binary Literals in Code and Spreadsheets
Tick Add 0b prefix and pick a grouping to paste results straight into source code. Most languages accept underscores between digits; C++ and C23 use an apostrophe instead.
| Language | Literal | Digit separator |
|---|---|---|
| Python | 0b11010110 | _ (Python 3.6+) |
| JavaScript | 0b11010110 | _ (ES2021); add n for BigInt past 53 bits |
| Java | 0b11010110 | _ (Java 7+) |
| C# | 0b11010110 | _ (C# 7.0+) |
| C++ | 0b11010110 | ' (C++14+) |
| C | 0b11010110 | ' (C23; earlier only as a compiler extension) |
| Go, Rust | 0b11010110 | _ |
Generate the same thing in code
# Python: 16 random bits, zero-padded
import secrets
format(secrets.randbits(16), '016b')
// JavaScript: one random byte as 8 binary digits
crypto.getRandomValues(new Uint8Array(1))[0].toString(2).padStart(8, '0')
' Excel: a random 8-bit number (DEC2BIN accepts -512 to 511 only)
=DEC2BIN(RANDBETWEEN(0, 255), 8)Excel's DEC2BIN stops at 10 bits, so for longer random binary in a spreadsheet, generate the numbers here and paste them as text. Format the column as Text first, or Excel will read 00001101 as the number 1101 and drop the zeros.
Frequently asked questions
How do I generate a random 8-bit binary number?
Leave Size by on Number of bits, set Bits each to 8 and Leading zeros to Keep. Every value from 00000000 to 11111111 (0 to 255) is equally likely.
Why do some results have fewer digits than I asked for?
Leading zeros is set to Drop them, so a result like 00101100 is shown as 101100. Switch to Keep for a fixed width, or Always start with 1 for numbers that genuinely need every digit.
Can I make a worksheet without the answers?
Yes. Untick Show decimal and hex, copy or print the binary column, then tick it again to see the answers for the same numbers. Changing a setting or pressing Generate new makes a new set.
Can I get a random binary number in a range like 1 to 100?
Yes. Switch Size by to Range of values and enter 1 and 100. Results are padded to 7 digits, the width of 100 (1100100), and every value is equally likely.
Sources
- MDN: Crypto.getRandomValues(): the browser’s cryptographically strong random source, and its 65,536-byte limit per call.
- NIST SP 800-22 Rev. 1a: A Statistical Test Suite for Random and Pseudorandom Number Generators: the frequency (monobit), runs and longest-run-of-ones tests.
- Mark F. Schilling, “The Longest Run of Heads”, The College Mathematics Journal 21(3), 1990: how long the longest run in a random sequence tends to be.
- Microsoft Support: DEC2BIN function: the −512 to 511 input limit and the places argument.
- Python documentation: secrets module: secrets.randbits(k).
- PEP 515: Underscores in Numeric Literals: digit separators in Python 3.6.
- MDN: JavaScript lexical grammar: binary literals, numeric separators and BigInt literals.
- cppreference: Integer literal: C++14 binary literals and the ' digit separator.
Related Tools
Create GUIDs for Microsoft and .NET fields.
Create a random pixel image and save it as a PNG.
Generate bytes in hex, Base64, or decimal format.
Pick a date between your chosen start and end dates.
Generate decimals with a chosen range and precision.
Generate fractions and see their simplified forms.