# Why do Zig, Rust and Julia give different random numbers when I give them the same seed and algorithm?

**URL:** <https://ziggit.dev/t/why-do-zig-rust-and-julia-give-different-random-numbers-when-i-give-them-the-same-seed-and-algorithm/17422>\
**Category:** Help\
**Tags:** standard-library\
**Created:** [August 30, 2026, 2:36pm UTC](https://ziggit.dev/t/why-do-zig-rust-and-julia-give-different-random-numbers-when-i-give-them-the-same-seed-and-algorithm/17422 "2026-08-30T14:36:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![allonsyeet](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/c6cbf5/32.png) [@allonsyeet](https://ziggit.dev/u/allonsyeet)\
**Post date:** [August 30, 2026, 2:36pm UTC](https://ziggit.dev/t/why-do-zig-rust-and-julia-give-different-random-numbers-when-i-give-them-the-same-seed-and-algorithm/17422/1 "2026-08-30T14:36:09Z")

</div>

I have this sample code snippet in Zig that generates 10 random `f32` and puts them in a slice.

```zig
const std = @import("std");

pub fn main() void {
    var prng_state = std.Random.Xoshiro256.init(0);
    const prng = prng_state.random();
    var slice: [10]f32 = undefined;
    
    for (0..slice.len) |idx| {
        slice[idx] = prng.float(f32);
    }
    
    std.debug.print("{any}\n", .{slice});
}

```

This outputs the following:

```zig
{ 0.27175805, 0.25162527, 0.30172718, 0.011619115, 0.3952554, 0.029364396, 0.7070683, 0.7960411, 0.26941678, 0.094871685 }

```

Same snippet in Rust

```rust
use rand::prelude::*;
use rand_distr::Uniform;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let rng = rand::rngs::Xoshiro256PlusPlus::from_seed([0u8; 32]);
    let arr: Vec<f32> = rng.sample_iter(Uniform::new(0.0, 1.0)?).take(10).collect();

    eprintln!("{arr:?}");
    Ok(())
}

```

Outputs this:

```zig
[0.3245752, 0.38223922, 0.3596171, 0.011455417, 0.49527, 0.020565152, 0.85724735, 0.8455087, 0.29488564, 0.07423377]

```

I checked to make sure that `std.Random.Xoshiro256` is actually the `Xoshiro256++` algorithm and the comment on the file states it is.

```zig
//! Xoshiro256++ - http://xoroshiro.di.unimi.it/
//!
//! PRNG

```

Just to double check, I used Julia to generate 10 random numbers too, and it seemed to give different numbers too, which makes me think it’s something I’m doing incorrectly.

```julia
julia> using Random

julia> rand(Xoshiro(0), Float32, 10)
10-element Vector{Float32}:
 0.48576927
 0.40569943
 0.017441511
 0.06854582
 0.2000143
 0.86214083
 0.27072555
 0.08597082
 0.30745035
 0.6616127

```

Any ideas what’s up? I wanted to get the sequence of random numbers to write tests for a function that depends on a prng.

---

<div class="post-metadata">

**Author:** ![alanza](https://ziggit.dev/user_avatar/ziggit.dev/alanza/32/6006_2.png) [@alanza](https://ziggit.dev/u/alanza)\
**Post date:** [August 30, 2026, 2:42pm UTC](https://ziggit.dev/t/why-do-zig-rust-and-julia-give-different-random-numbers-when-i-give-them-the-same-seed-and-algorithm/17422/2 "2026-08-30T14:42:20Z")

</div>

Read the source code? It’s possible that the seed you give is transformed in some deterministic yet different way by each language’s implementation.

---

<div class="post-metadata">

**Author:** ![IntegratedQuantum](https://ziggit.dev/user_avatar/ziggit.dev/integratedquantum/32/782_2.png) [@IntegratedQuantum](https://ziggit.dev/u/IntegratedQuantum)\
**Post date:** [August 30, 2026, 2:48pm UTC](https://ziggit.dev/t/why-do-zig-rust-and-julia-give-different-random-numbers-when-i-give-them-the-same-seed-and-algorithm/17422/3 "2026-08-30T14:48:27Z")

</div>

The way it generates float is probably different. Have you tried just integers, or ideally just bytes?

---

<div class="post-metadata">

**Author:** ![allonsyeet](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/c6cbf5/32.png) [@allonsyeet](https://ziggit.dev/u/allonsyeet)\
**Post date:** [August 30, 2026, 3:07pm UTC](https://ziggit.dev/t/why-do-zig-rust-and-julia-give-different-random-numbers-when-i-give-them-the-same-seed-and-algorithm/17422/4 "2026-08-30T15:07:46Z")

</div>

Ahh makes sense. That seems to have been the issue. Here’s the output from Rust and Zig. Couldn’t get the same output from Julia but that’s maybe because there’s more stuff going on in between.

```rust
use rand::prelude::*;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let mut rng = rand::rngs::Xoshiro256PlusPlus::from_seed([0u8; 32]);
    let mut buff: [u8; 10] = [0u8; 10];
    rng.fill_bytes(&mut buff);

    eprintln!("{buff:?}");
    Ok(())
}

```

Outputs:

```zig
[223, 35, 11, 73, 97, 93, 23, 83, 7, 213]

```

And zig:

```zig
const std = @import("std");

pub fn main() !void {
    var prng_state = std.Random.Xoshiro256.init(0);
    const prng = prng_state.random();

    var buff: [10]u8 = undefined;
    prng.bytes(&buff);

    std.debug.print("{any}\n", .{buff});
}

```

Outputs:

```zig
{ 223, 35, 11, 73, 97, 93, 23, 83, 7, 213 }

```

tysm! \<3

---

<div class="post-metadata">

**Author:** ![castholm](https://ziggit.dev/user_avatar/ziggit.dev/castholm/32/1330_2.png) [@castholm](https://ziggit.dev/u/castholm)\
**Post date:** [August 30, 2026, 4:14pm UTC](https://ziggit.dev/t/why-do-zig-rust-and-julia-give-different-random-numbers-when-i-give-them-the-same-seed-and-algorithm/17422/5 "2026-08-30T16:14:24Z")

</div>

I was curious so for anyone else who wonders, the Rust code generates floats in a way equivalent to the following:

```zig
const std = @import("std");

pub fn main() void {
    var prng_state = std.Random.Xoshiro256.init(0);
    const prng = prng_state.random();
    var slice: [10]f32 = undefined;

    for (0..slice.len) |idx| {
        const significand_bits: u32 = @truncate(prng.int(u64) >> (64 - 23));
        const exponent_bits: u32 = @bitCast(@as(f32, 1));
        const one_two: f32 = @bitCast(significand_bits | exponent_bits);
        const zero_one: f32 = one_two - 1;
        slice[idx] = zero_one;
    }

    std.debug.print("{any}\n", .{slice});
}

```

```zig
{ 0.3245752, 0.38223922, 0.3596171, 0.011455417, 0.49527, 0.020565152, 0.85724735, 0.8455087, 0.29488564, 0.07423377 }

```

This is uniformly distributed and consumes 64 bits from the RNG (only 23 bits of randomness are required), but covers only a tiny fraction of all unique float values in the [0, 1) range (223 unique values out of 127 × 223 possible).

The Zig [`Random.float`](https://ziglang.org/documentation/master/std/#std.Random.float) implementation is uniformly distributed and covers all unique values, but may rarely consume more than 64 bits from the RNG (the worst case requires 149 bits of randomness).
