1.6.1 • Published 7 months ago

uny-random v1.6.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

UnyRandom npm version license


This package is not sponsored by or affiliated with Unity Technologies or its affiliates. "Unity" is a trademark or registered trademark of Unity Technologies or its affiliates in the U.S. and elsewhere. All the code in this package is only made to mirror the behavior of the original PRNG and is not to be seen as a representation of the original source code owned by Unity Technologies.

Quick start

Install the uny-random npm package:

npm install uny-random # or via any other package manager you might use such as yarn or pnpm

The package can then be easily added to your project via ES6 import:

import unyRandom from 'UnyRandom'; // For a static version of the PRNG
// or
import { UnyRandom } from 'UnyRandom'; // To make the UnyRandom parent class available in case you need multiple copies

The randomizer can also be added directly to a website (mini version is ES5 backwards compatible), and will be globally available as an instance called unyRandom

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/uny-random@1.6.1/dist/uny-random.min.js"></script>

Features

UnyRandom is made to replicate the behavior of all the methods and properties attached to the UnityEngine.Random class and all UnityEngine.Random. If you find something missing, please reach out!

Example usage

const unyRandom = new UnyRandom();
unyRandom.value; // => 0.8190207
unyRandom.initState(926378578) // => new state = [926378578, 4043363419, 3111484136, 1182246281]
unyRandom.range(-1200, 123544); // => 2494
unyRandom.range(-10.5, 99.6); // => 11.33
unyRandom.state; // => [255129647,  4127199628, 2722027581, 1244591378] 
unyRandom.insideUnitCircle; // => { x: -0.5568735, y: 0.3252837 }
unyRandom.colorHSV(0.18, 0.85, 0.38, 1, 0, 0.05, 0, 1) // => { r: 0.001949894, g: 0.002597082, b: 0.005258002, a: 0.05327214 }
unyRandom.rotationUniform; // => { x: -0.6366615, y: -0.7671657, z: -0.04677578, w: 0.062698 }

there are also some additional functionality added such as a basic fluent interface, the ability to skip forward, getting the UInt value all the test of values are based on via .next and specifying if you want an intRange or floatRange

unyRandom.initState(11288839).value; // => 0.9660726
unyRandom.skip(5).value; // => 0.3456783
unyRandom.rangeInt(-100, 200); // => 78
unyRandom.rangeFloat(-100, 200); // => 149.5162
unyRandom.next; // => 1434251478

Contributing

  • Missing something or found a bug? Report here.
  • Want to contribute? UnyRandom is mostly feature complete, so unless there is an update from Unity, there is not much more or add. But if you have anything feel free to reach out!

FAQ

Where has Unity published this code?

They have not. Their runtime is closed source.

How did you manage to decompile the C++ Runtime?

I did not decompile anything. UnyRandom was created from open information made public by Unity Technologies themselves

Closed source and no decompilation? How did yo make it then?

Trial and error. This project has been 100% test driven with test cases being created based on results from Unity and testing various PRNG implementations based on the data till the tests pass. TDD!

If you did not use Unity's code directly, how can we be sure it's correct?

There are 350 different test cases added to this relativly small project for that reason, to make sure that UnyRandom properly reflects the behaviour of UnityEngine.Random. Feel free to check them out!

I'm getting some weird rounding errors when I'm asking for large floats! From the range [-861106178.1, 1772860600.99], Unity is giving me 195726688, but UnyRandom is giving me 195726687. What gives!=

This is an effect of UnityEngine.Random being made in C++ and UnyRandom using JavaScript. C++ is using extended precision for floating point arithmetic (80 bit during calculation, rounding it down to a 32 bit at the end), while JS is natively always working with 64 bit floats. So you get some very, very small differences sometimes. I've added as much sane rounding as possible to replicate the result of UnityEngine.Random without going complete overkill and in the end it's accurate to 99.999995%.
It's virtually identical and the risk of getting invalid results is insignificant

1.6.1

7 months ago

1.6.0

7 months ago

1.0.1

2 years ago

1.0.0

2 years ago