Random Number Generator

Generate random integers in a custom range, with options for multiple values and unique picks.

Back to all tools on ToolForge

More in Generators

Settings

Min: Max:

How many numbers:

Allow duplicates?

Result

Ready.

About Random Number Generator

This random number generator creates integers in a custom range using uniform distribution. It supports generating multiple values with options for unique picks (sampling without replacement) or allowing duplicates (sampling with replacement). The tool uses crypto.getRandomValues() when available for cryptographically secure random number generation.

It is useful for lottery and raffle number selection, classroom activities, test data generation, statistical sampling, simulation inputs, game mechanics, scientific experiments, and randomized controlled trials.

Random Number Generation Methods

Two approaches for generating random numbers:

Random Sources:

1. crypto.getRandomValues (Secure)
   - Uses OS entropy pool
   - Cryptographically secure PRNG
   - Uniform distribution guaranteed
   - Recommended for fair outcomes

   Implementation:
   const array = new Uint32Array(1);
   crypto.getRandomValues(array);
   const random = array[0] / 0xFFFFFFFF;
   const number = min + Math.floor(random * range);

2. Math.random (Standard)
   - Pseudo-random algorithm
   - Fast, suitable for casual use
   - Implementation-dependent quality

   Implementation:
   const random = Math.random();
   const number = min + Math.floor(random * range);

Range Mapping:
  Given range [min, max]:
  rangeSize = max - min + 1
  randomNumber = min + floor(random × rangeSize)

  Example: range [1, 100]
  rangeSize = 100 - 1 + 1 = 100
  randomNumber = 1 + floor(random × 100)
  Result: integer from 1 to 100

Sampling Methods

Property With Replacement Without Replacement
Duplicates Allowed Not allowed
Probability Same for each pick Changes after each pick
Independence Independent trials Dependent trials
Max picks Unlimited Limited to range size
Use case Dice rolls, lottery Raffles, sampling
Algorithm Direct random Pool selection

Probability Reference

With Replacement (Duplicates Allowed):

For range [1, N] with k picks:
  - Each number has probability 1/N per pick
  - Probability of specific number appearing at least once:
    P = 1 - (1 - 1/N)^k

Example: Range [1, 100], 10 picks
  P(any specific number) = 1 - (0.99)^10 = 0.0956 = 9.56%

Without Replacement (Unique Picks):

For range [1, N] with k picks (k ≤ N):
  - First pick: probability 1/N
  - Second pick: probability 1/(N-1)
  - k-th pick: probability 1/(N-k+1)

Example: Range [1, 10], picking 3 numbers
  P(first pick = 5) = 1/10 = 10%
  P(second pick = 7 | first ≠ 7) = 1/9 = 11.1%
  P(third pick = 3 | first two ≠ 3) = 1/8 = 12.5%

Expected Statistics (1000 trials, range 1-10):

Number | Expected Count | Expected Frequency
-------|----------------|-------------------
1      | 100            | 10.0%
2      | 100            | 10.0%
3      | 100            | 10.0%
4      | 100            | 10.0%
5      | 100            | 10.0%
6      | 100            | 10.0%
7      | 100            | 10.0%
8      | 100            | 10.0%
9      | 100            | 10.0%
10     | 100            | 10.0%

Common Use Cases

Use Case Typical Range Duplicates Count
Lottery Pick 1-49 or 1-69 No 5-7
Dice Roll (d6) 1-6 Yes 1-5
Dice Roll (d20) 1-20 Yes 1
Raffle Drawing 1-ticket count No Winner count
Classroom Selection 1-class size No 1 or more
Test Data Variable Yes As needed
Percentage 1-100 Yes 1
Coin Flip 1-2 Yes 1

Code Examples by Language

JavaScript:
  // Single random number in range
  function randomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  // Multiple unique numbers
  function randomUnique(min, max, count) {
    const pool = [];
    for (let i = min; i <= max; i++) pool.push(i);
    const result = [];
    for (let i = 0; i < count; i++) {
      const idx = Math.floor(Math.random() * pool.length);
      result.push(pool[idx]);
      pool.splice(idx, 1);
    }
    return result;
  }

  // Using crypto (secure)
  function secureRandomInt(min, max) {
    const array = new Uint32Array(1);
    crypto.getRandomValues(array);
    const range = max - min + 1;
    return min + (array[0] % range);
  }

Python:
  import random

  # Single number
  random.randint(1, 100)

  # Multiple with duplicates
  [random.randint(1, 100) for _ in range(5)]

  # Multiple unique
  random.sample(range(1, 101), 5)

  # Secure random
  import secrets
  secrets.randbelow(100) + 1

PHP:
  // Single number
  rand(1, 100);

  // Secure (PHP 7+)
  random_int(1, 100);

  // Multiple unique
  $numbers = range(1, 100);
  shuffle($numbers);
  $result = array_slice($numbers, 0, 5);

Java:
  import java.util.Random;
  import java.security.SecureRandom;

  Random rand = new Random();
  int num = rand.nextInt(100) + 1;  // 1-100

  // Unique numbers
  List numbers = IntStream.rangeClosed(1, 100)
      .boxed().collect(Collectors.toList());
  Collections.shuffle(numbers);
  List result = numbers.subList(0, 5);

  // Secure
  SecureRandom secure = new SecureRandom();
  int num = secure.nextInt(100) + 1;

C#:
  var rand = new Random();
  int num = rand.Next(1, 101);  // 1-100

  // Unique numbers
  var numbers = Enumerable.Range(1, 100).OrderBy(x => rand.Next()).Take(5);

  // Secure
  using System.Security.Cryptography;
  var rng = RandomNumberGenerator.Create();
  var data = new byte[4];
  rng.GetBytes(data);
  int num = BitConverter.ToInt32(data, 0) % 100 + 1;

Random Number Examples

Example 1: Single Number (1-100)
  Result: 47
  Use: General purpose, percentage

Example 2: Lottery Pick (1-49, 6 unique)
  Result: 7, 15, 23, 31, 42, 48
  Use: Lottery simulation

Example 3: Dice Roll (1-6)
  Result: 4
  Use: Board game

Example 4: Multiple Dice (1-6, 3 dice)
  Result: 2, 5, 3 (sum: 10)
  Use: RPG damage roll

Example 5: d20 Roll (1-20)
  Result: 17
  Use: D&D attack roll

Example 6: Coin Flip (1-2)
  Result: 1 (Heads)
  Use: Binary decision

Example 7: Classroom Pick (1-30, unique)
  Result: 14
  Use: Select student

Example 8: Test Data (1-1000, 10 values)
  Result: 234, 567, 123, 890, 456, 789, 321, 654, 987, 432
  Use: Sample dataset

Example 9: Raffle Winners (1-500, 3 unique)
  Result: 127, 345, 89
  Use: Prize drawing

Example 10: Negative Range (-10 to 10)
  Result: -3
  Use: Temperature offset

Fisher-Yates Shuffle

The Fisher-Yates shuffle generates a random permutation
of a finite sequence. It's used for unique number selection.

Algorithm:
  1. Create array [min, min+1, ..., max]
  2. For i from n-1 down to 1:
     a. Pick random j from 0 to i
     b. Swap array[i] and array[j]
  3. Take first k elements

JavaScript Implementation:
  function fisherYates(array) {
    for (let i = array.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
  }

  // Get k unique numbers from range
  function pickUnique(min, max, k) {
    const pool = [];
    for (let i = min; i <= max; i++) pool.push(i);
    return fisherYates(pool).slice(0, k);
  }

Time Complexity: O(n) where n = range size
Space Complexity: O(n) for the pool array

Best Practices

Limitations

Frequently Asked Questions

How does random number generation work?
Random number generators use either pseudo-random algorithms (Math.random) or cryptographically secure sources (crypto.getRandomValues). The tool maps random values to your specified range using modular arithmetic. For unique picks, it uses a Fisher-Yates style selection from a pool of available numbers.
What is the difference between allowing and disallowing duplicates?
With duplicates allowed, each number is independently selected, so the same value can appear multiple times. Without duplicates, each number can only be selected once, using sampling without replacement. This affects probability: with duplicates, each pick has equal chance; without, probabilities change after each pick.
What is a uniform distribution?
A uniform distribution means every number in the range has equal probability of being selected. For range 1-100, each number has 1% chance. Over many trials, each number should appear approximately the same number of times. This tool ensures uniform distribution across the specified range.
How do I generate random numbers for cryptographic purposes?
This tool uses crypto.getRandomValues() when available for secure random number generation. However, for cryptographic keys, tokens, or passwords, use dedicated tools like the Password Generator or Random String Generator, which are designed for security-sensitive applications with appropriate character sets and entropy.
What are common use cases for random number generation?
Common uses include: lottery and raffle number selection, classroom activities and student selection, test data generation, statistical sampling, simulation inputs, game mechanics (dice rolls, damage calculation), scientific experiments, and randomized controlled trials.
How is this different from a dice roll?
A standard die produces numbers 1-6 with equal probability. This tool can generate any range (e.g., 1-100, -50 to 50). Dice are physical objects subject to wear and bias; digital generators use mathematical algorithms. Multiple dice can be simulated by generating multiple numbers and summing.