---
title: "How PyTorch Generates Random Numbers in Parallel on the GPU"
url: https://daily.dev/posts/how-pytorch-generates-random-numbers-in-parallel-on-the-gpu-mik0spyux
source_url: https://blog.codingconfessions.com/p/how-pytorch-generates-random-numbers
type: article
source: "Confessions of a Code Addict"
published: 2025-12-18T10:30:31.143Z
updated: 2026-03-15T04:53:40.928Z
tags: ["algorithms", "c++", "cuda", "gpu", "pytorch"]
reading_time: 20
upvotes: 1
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How PyTorch Generates Random Numbers in Parallel on the GPU

**[Confessions of a Code Addict](https://daily.dev/sources/codeconfessions)** · 20 min read · 1 upvotes · 0 comments

## Summary

Philox is a counter-based parallel random number generator that enables PyTorch to generate millions of random numbers simultaneously on GPUs. Unlike traditional sequential PRNGs that depend on previous state, Philox uses a cryptographic-like construction to transform a 128-bit counter into pseudorandom output through 10 rounds of multiplication, XOR operations, and permutation. The counter space is partitioned into subsequences (upper 64 bits) and offsets (lower 64 bits), allowing thousands of GPU threads to independently generate random numbers without coordination. PyTorch's implementation maintains only 44 bytes of state per generator instance and efficiently batches 4 numbers at a time, making it ideal for deep learning operations like weight initialization, dropout, and stochastic gradient descent.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://blog.codingconfessions.com/p/how-pytorch-generates-random-numbers>

## Similar posts on daily.dev

- [This \(Pseudo\) Random Number Generator Does It With Neon](https://daily.dev/posts/this-pseudo-random-number-generator-does-it-with-neon-zyrc6p4gw) · Hackaday · 0 upvotes · 0 comments

---

Tags: [#algorithms](https://daily.dev/tags/algorithms), [#c++](https://daily.dev/tags/c++), [#cuda](https://daily.dev/tags/cuda), [#gpu](https://daily.dev/tags/gpu), [#pytorch](https://daily.dev/tags/pytorch)

[View this post on daily.dev](https://daily.dev/posts/how-pytorch-generates-random-numbers-in-parallel-on-the-gpu-mik0spyux)
