▶ Random Everything - 100+ Free Generators // v2.5.0
root@generate-random:~/random-number-code$ _

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
Pythonrandom.randint(1, 10)secrets.randbelow(10) + 1
JavaScriptMath.floor(Math.random() * 10) + 1crypto.getRandomValues()
JavaThreadLocalRandom.current().nextInt(1, 11)new SecureRandom().nextInt(10) + 1
C#Random.Shared.Next(1, 11)RandomNumberGenerator.GetInt32(1, 11)
Gorand.IntN(10) + 1 // math/rand/v2crypto/rand: rand.Int(rand.Reader, big.NewInt(10))
PHPmt_rand(1, 10)random_int(1, 10)
Bashecho $(( 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? ▶
Every language has a built-in function: random.randint in Python, Math.random in JavaScript, ThreadLocalRandom in Java, Random.Shared in C#, math/rand/v2 in Go, random_int in PHP and $RANDOM in Bash. Use the secure variant for anything security-related.
Which random functions are cryptographically secure? ▶
Python secrets, JavaScript crypto.getRandomValues, Java SecureRandom, C# RandomNumberGenerator, Go crypto/rand, PHP random_int and random_bytes, and /dev/urandom on Linux and macOS.
Why is my random number never equal to the maximum? ▶
Many APIs exclude the upper bound - Java nextInt, C# Next, Go IntN, Python randrange. Add 1 to the maximum to include it.

[ HOW TO CITE THIS PAGE ]

APA Style:
Generate-Random.org. (2026). Random Number Code Generator. Retrieved from https://generate-random.org/random-number-code
Web Citation:
Random Number Code Generator - Generate-Random.org (https://generate-random.org/random-number-code)