Random Number Code Generator
Ready-to-paste code for a random number in any range - Python, JavaScript, Java, C#, Go, PHP and Bash, secure or not
Need a random number in code? Pick a language and a range above and copy a correct, ready-to-run snippet - the fast version for games and tests, or the cryptographically secure one for tokens and draws.
Random Number Between 1 and 10 in Every Language
The fast way and the secure way, side by side. Each language page has full examples: floats, picking from a list, shuffling and common mistakes.
| Language | Fast (not secure) | Cryptographically secure |
|---|---|---|
| Python | random.randint(1, 10) | secrets.randbelow(10) + 1 |
| JavaScript | Math.floor(Math.random() * 10) + 1 | crypto.getRandomValues() |
| Java | ThreadLocalRandom.current().nextInt(1, 11) | new SecureRandom().nextInt(10) + 1 |
| C# | Random.Shared.Next(1, 11) | RandomNumberGenerator.GetInt32(1, 11) |
| Go | rand.IntN(10) + 1 // math/rand/v2 | crypto/rand: rand.Int(rand.Reader, big.NewInt(10)) |
| PHP | mt_rand(1, 10) | random_int(1, 10) |
| Bash | echo $(( RANDOM % 10 + 1 )) | shuf -i 1-10 -n 1 --random-source=/dev/urandom |
Secure vs Non-Secure Random Numbers
Every language ships a fast pseudo-random generator (Mersenne Twister, xorshift, PCG...) that is perfect for games, simulations and tests - and predictable: seen enough outputs, an attacker can work out the next ones. For passwords, tokens, session IDs, invite codes or prize draws, use the language's cryptographically secure generator (CSPRNG), which reads from the operating system. It is a little slower and just as easy to call.
Inclusive or Exclusive Upper Bound?
The most common bug in random-number code is an off-by-one on the maximum. Python's randint, PHP's random_int and mt_rand include it; Java's nextInt, C#'s Next and GetInt32, Go's IntN, Node's randomInt and Python's randrange exclude it, so you add 1. The generator above always produces an inclusive range and says so in a comment.
Frequently Asked Questions
How do I generate a random number in code? ▶
Which random functions are cryptographically secure? ▶
Why is my random number never equal to the maximum? ▶
[ HOW TO CITE THIS PAGE ]
Generate-Random.org. (2026). Random Number Code Generator. Retrieved from https://generate-random.org/random-number-code
Random Number Code Generator - Generate-Random.org (https://generate-random.org/random-number-code)