npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

uny-random

v1.6.1

Published

JavaScript implementation of the PRNG used by the Unity game engine.

Downloads

16

Readme

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/[email protected]/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