uniqid

uniqid(string $prefix = "", bool $moreEntropy = false): string

Generates a unique ID based on the microsecond timestamp (with optional prefix and extra entropy).

C C23

C23
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main(void) {
    /* no uuid in the C standard library: hand-rolled timestamp + rand idiom */
    char id[32];
    snprintf(id, sizeof id, "%lx-%04x",
             (unsigned long)time(NULL), rand() % 0xffff);
    printf("%d chars (timestamp-random)\n", (int)strlen(id));
    return 0;
}
Output
13 chars (timestamp-random)

C++ C++26

C++26
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>

int main() {
    // no uuid in the C++ standard library: hand-rolled timestamp + rand idiom
    char id[32];
    std::snprintf(id, sizeof id, "%lx-%04x",
                  (unsigned long)std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()),
                  std::rand() % 0xffff);
    std::printf("%d chars (timestamp-random)\n", (int)std::strlen(id));
    return 0;
}
Output
13 chars (timestamp-random)

C# v11.0.100

v11.0.100
Guid id = Guid.NewGuid(); // random 128-bit guid: the .NET counterpart of a UUID
Console.WriteLine($"random guid, {id.ToString("N").Length} hex digits");
Output
random guid, 32 hex digits

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"math/rand"
	"strings"
	"time"
)

func main() {
	// no uuid package in the standard library: the idiom is nanosecond timestamp + random
	id := fmt.Sprintf("%016x-%04x", time.Now().UnixNano(), rand.Intn(0xffff))
	fmt.Printf("%d chars, %d dash\n", len(id), strings.Count(id, "-"))
}
Output
21 chars, 1 dash

Java v27.0.1

v27.0.1
import java.util.UUID;

public class Main {
    public static void main(String[] args) {
        UUID id = UUID.randomUUID(); // random (v4) UUID, built into the JDK
        System.out.println("v" + id.version() + " uuid, "
            + id.toString().replace("-", "").length() + " hex digits");
    }
}
Output
v4 uuid, 32 hex digits

JavaScript v26.13.1

v26.13.1
const { randomUUID } = require("node:crypto"); // CSPRNG-backed v4 UUID
const id = randomUUID();
console.log(`${id.length} chars incl. ${id.split("-").length - 1} dashes`);
Output
36 chars incl. 4 dashes

PHP v8.6.0

v8.6.0
// uniqid() derives from the microsecond clock: the value differs every run,
// so the example asserts the fixed shape instead of printing the value
$id = uniqid('inv_'); // prefix + 13 hex digits (seconds + microseconds)
echo substr($id, 0, 4), '+', strlen($id) - 4, ' hex digits';
Output
inv_+13 hex digits

Python v3.15.2

v3.15.2
import uuid

u = uuid.uuid4()  # random (v4) UUID, standard library
print(f"v{u.version} uuid, {len(u.hex)} hex digits")
Output
v4 uuid, 32 hex digits

Rust v1.99.1

v1.99.1
// uuid crate (v4 feature): the de-facto standard for random UUIDs in Rust
fn main() {
    let id = uuid::Uuid::new_v4();
    println!(
        "v{} uuid, {} hex digits",
        id.get_version_num(),
        id.simple().to_string().len()
    );
}
Output
v4 uuid, 32 hex digits

TypeScript v26.13.1

v26.13.1
import { randomUUID } from "node:crypto"; // CSPRNG-backed v4 UUID
const id: string = randomUUID();
console.log(`${id.length} chars incl. ${id.split("-").length - 1} dashes`);
Output
36 chars incl. 4 dashes

Language differences

C: no uuid in the standard library — the example hand-rolls a "second timestamp + rand" concatenation, the real C-ecosystem idiom; it stays pseudo-random across processes/machines (an unseeded rand even repeats every run), so production code should pull in libuuid or read /dev/urandom.

C++: same as C — the standard library still has no uuid (the example hand-rolls the concatenation); Boost.UUID or std::format + random_device are the usual combinations.

C#: Guid.NewGuid() (example) is the .NET UUID (random flavor) via CoCreateGuid on Windows; note Guid exposes no version field, but the semantics match RFC 4122 v4.

Go: no uuid package in the standard library (the example hand-rolls nanosecond-timestamp + rand); production projects usually pull in github.com/google/uuid — uuid.NewString() in one line.

Java: UUID.randomUUID() (example) is built into the JDK and returns RFC 4122 v4; version() reads the version back, handy for asserting "this is a random UUID".

JavaScript: crypto.randomUUID() (example, Node 14.17+/browsers in secure contexts) is a CSPRNG-backed v4 UUID, always 36 chars with 4 hyphens; in browsers it only exists on HTTPS/localhost.

PHP: uniqid is microsecond-timestamp-based (the example asserts the fixed shape of prefix + 13 hex digits) — two processes calling within the same microsecond get the same value (the collision limitation); more_entropy = true appends an LCG decimal tail, which mitigates but does not fix it; it is predictable (the timestamp can be estimated), so never use it as a security token — tokens are random_bytes, globally unique identifiers are the UUID options above.

Python: uuid.uuid4() (example) is built into the standard library; when you want a business prefix like uniqid's, just concatenate strings — uuid itself takes no prefix.

Rust: no uuid in the standard library; the ecosystem standard is the uuid crate's Uuid::new_v4() (example); the simple()/hyphenated() adapters control the output shape.

TypeScript: same as JavaScript; across all random-flavored entries on this site the examples assert shape/statistics instead of printing concrete values, keeping outputs deterministic.