https://github.com/minavoii/unity-random
A reimplementation of Unity's pseudo-random number generator.
https://github.com/minavoii/unity-random
prng random rng rust unity unity-random
Last synced: 4 months ago
JSON representation
A reimplementation of Unity's pseudo-random number generator.
- Host: GitHub
- URL: https://github.com/minavoii/unity-random
- Owner: minavoii
- License: mit
- Created: 2024-08-22T17:13:41.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-23T16:20:52.000Z (almost 2 years ago)
- Last Synced: 2025-10-28T00:01:30.016Z (8 months ago)
- Topics: prng, random, rng, rust, unity, unity-random
- Language: Rust
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Unity-Random
[
](https://github.com/minavoii/unity-random) [
](https://crates.io/crates/unity-random) [
](https://docs.rs/unity-random)
A full reimplementation of Unity's PRNG in Rust, which is based on the Xorshift128 algorithm.
Results should be near 1-to-1 with Unity, with the exception of `color()` which may produce slightly inaccurate results due to an issue with .NET versions before 5.x.
Unlike Unity, it does not offer a static class.
Note: you should not use this if you expect a cryptographically secure PRNG. If you just want to generate random numbers, you should be looking into the [`Rand`](https://crates.io/crates/rand) crate.
This project is not affiliated with Unity Technologies.
## Usage
```rust
use unity_random::Random;
let mut random = Random::new();
random.init_state(220824); // set the PRNG seed
let integer = random.range_int(0, 100);
let float = random.range_float(0., 1.);
let rotation = random.rotation_uniform();
```
You can also save/load the current state:
```rust
use unity_random::Random;
let mut random = Random::new();
random.init_state(220824);
// You can save the current state...
let saved_state = random.state;
// then generate random numbers...
let i1 = random.range_int(0, 100);
// and load the state again...
random.state = saved_state;
// ... to generate the same sequence
let i2 = random.range_int(0, 100);
assert_eq!(i1, i2);
```
## Feature flags
- `serde`: Enables serialization and deserialization of the PRNG's `State`.