โ† Back to Blog

How to Bulk Generate UUIDs

2026-04-12 ยท 5 min read

โ† Back to Blog

How to Bulk Generate UUIDs

ยท 5 min read

Common Scenarios for Bulk UUID Generation

Typical scenarios requiring bulk UUID generation: database seed data initialization (pre-preparing unique IDs for large numbers of records rather than having the database generate them at insert time); test data generation (preparing a fixed UUID set for automated tests to make tests deterministic); data migration (batch-assigning new UUIDs to records migrated from old systems); API key or token candidate pools (bulk pre-generating backup unique identifiers); batch naming of documents or assets (assigning unique names to a batch of uploaded files).

Command-Line Bulk Generation

# Linux/macOS๏ผš็”Ÿๆˆ 100 ไธช UUID
for i in $(seq 1 100); do uuidgen; done

# macOS ้ป˜่ฎคๅคงๅ†™๏ผŒ่ฝฌๅฐๅ†™
for i in $(seq 1 100); do uuidgen | tr '[:upper:]' '[:lower:]'; done

# ไฟๅญ˜ๅˆฐๆ–‡ไปถ
for i in $(seq 1 1000); do uuidgen; done > uuids.txt

# Python ไธ€่กŒๅ‘ฝไปคๆ‰น้‡็”Ÿๆˆ
python3 -c "import uuid; [print(uuid.uuid4()) for _ in range(100)]"

# ็”Ÿๆˆ CSV ๆ ผๅผ๏ผˆๅธฆ็ดขๅผ•๏ผ‰
python3 -c "
import uuid
print('index,uuid')
for i in range(1, 101):
    print(f'{i},{uuid.uuid4()}')
" > uuids.csv

# Node.js ๆ‰น้‡็”Ÿๆˆ
node -e "
const {randomUUID} = require('crypto');
for(let i=0;i randomUUID());
}

// ็”Ÿๆˆ JSON ๆ–‡ไปถ๏ผˆ้€‚ๅˆไฝœไธบๆต‹่ฏ•ๆ•ฐๆฎ๏ผ‰
function generateTestData(count) {
  const data = Array.from({ length: count }, (_, i) => ({
    id: randomUUID(),
    name: `Test Item ${i + 1}`,
    createdAt: new Date().toISOString(),
  }));
  return JSON.stringify(data, null, 2);
}

// ไฟๅญ˜ๅˆฐๆ–‡ไปถ
fs.writeFileSync('test-data.json', generateTestData(100));
console.log('Generated 100 test records with UUIDs');

// ็”Ÿๆˆ CSV ๆ ผๅผ
function generateCSV(count) {
  const header = 'id,name,status';
  const rows = Array.from({ length: count }, (_, i) =>
    `${randomUUID()},Item ${i + 1},active`
  );
  return [header, ...rows].join('\n');
}

fs.writeFileSync('test-data.csv', generateCSV(500));

Database-Level Bulk Generation

-- PostgreSQL๏ผšๆ‰น้‡็”Ÿๆˆ UUID ่ฎฐๅฝ•
INSERT INTO products (id, name, category)
SELECT gen_random_uuid(), 'Product ' || gs, 'default'
FROM generate_series(1, 1000) AS gs;

-- MySQL 8.0+๏ผšๆ‰น้‡ๆ’ๅ…ฅ
DELIMITER //
CREATE PROCEDURE bulk_insert_uuids(IN count INT)
BEGIN
  DECLARE i INT DEFAULT 0;
  WHILE i < count DO
    INSERT INTO items (id, name) VALUES (UUID(), CONCAT('Item ', i));
    SET i = i + 1;
  END WHILE;
END //
DELIMITER ;
CALL bulk_insert_uuids(1000);

-- SQL Server๏ผšๆ‰น้‡็”Ÿๆˆ
INSERT INTO orders (id, status)
SELECT NEWID(), 'pending'
FROM (
  SELECT TOP 1000 ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS n
  FROM sys.objects CROSS JOIN sys.objects
) AS nums;

Considerations for Bulk Generation

Things to note when bulk generating UUIDs: each UUID is independently generated with no correlation between them โ€” bulk generation does not increase collision risk; do not use loops calling non-cryptographically-secure random number generators like Math.random(); if bulk-generated UUIDs will be used in databases, consider batch inserts (rather than row-by-row inserts) for better performance; for extremely large batches (over one million), consider processing in chunks and writing to files to avoid memory overflow; if you need reproducible test data (same UUIDs generated each run), use UUID v5 derived from indexes or names rather than random v4.

Verifying Uniqueness of Bulk Generation Results

import uuid

# ็”Ÿๆˆๅนถ้ชŒ่ฏๅ”ฏไธ€ๆ€ง๏ผˆ็”จไบŽๆผ”็คบ๏ผŒ็”Ÿไบงไธญๆ— ้œ€่ฟ™ๆ ทๅš๏ผ‰
def bulk_generate_verified(count):
    uuids = set()
    while len(uuids) < count:
        new_uuid = str(uuid.uuid4())
        uuids.add(new_uuid)
    return list(uuids)

# ็ปŸ่ฎก set ๅคงๅฐ็ญ‰ไบŽ list ๅคงๅฐ่ฏดๆ˜Žๆ— ้‡ๅค
uuids = [str(uuid.uuid4()) for _ in range(100000)]
unique_count = len(set(uuids))
print(f"Generated: {len(uuids)}, Unique: {unique_count}")
# ๅ‡ ไนŽๅฟ…็„ถ่พ“ๅ‡บ๏ผšGenerated: 100000, Unique: 100000

Try the online tool now โ€” no installation, completely free.

Open Tool โ†’

Try the free tool now

Use Free Tool โ†’