Getting A Random Between Two Doubles

Apparently in regular Javascript, there is a Math.Random() function that returns a double value between 0.0 and 1.0 so that I can get a random value between incredibly large or small values.

Unity doesn’t know what it is, though. This is the code I’m using.

class ObjectClass {

var min_amount  : System.Double = 1 / System.Double.MaxValue;
var max_amount : System.Double = System.Double.MaxValue;
var range : System.Double;
var amount  : System.Double;


var ObjectProperties : ObjectClass;

function Start () {

ObjectProperties.range = ObjectProperties.max_amount - ObjectProperties.min_amount;
ObjectProperties.amount = (Math.Random() * ObjectProperties.range) + ObjectProperties.max_amount;


Now according to this site Math.Random() should be valid, but it doesn’t look like Unity uses it. Is there any way to get a random between two System.Double values in Unity?

Try using Random.Range. It uses floats though.

Unity doesn’t use Javascript (rather Unityscript, which is a custom language), and the site you linked to is about Java, which is an entirely 100% different language, and Unity does not use that, either. Unity scripting is built .NET/Mono, so use System.Math.Random. .NET uses doubles for math stuff. (Fun fact: Mathf functions in Unity typically just call System.Math functions, while converting doubles to singles.) BTW, you can just write “var min_amount : double”.

The whole Unity engine is designed for single floating point values. For most cases in a 3d engine, single precision is enough. Also the GPU hardware is optimised for 32-bit single precision floating point values.

So while Unity could have provided an additional random method which returns double values it just wouldn’t fit the rest of the API.

Anyways you still can use the random number generator that comes with the .NET / Mono framework. Since UnityScript is also a .NET / Mono language you have access to the same classes as C# has.

The System.Random class requires you to create an instance of the class in order to use it. You can create a static wrapper class like this:

// DRandom.cs
using System;

public static class DRandom
    private static Random m_Rnd ;
    private static int m_LastSeed;
    static DRandom()
        var v = DateTime.Now.Ticks;
        m_LastSeed = (int)(v ^ (v >> 7) ^ (v >> 17));
        m_Rnd = new Random(m_LastSeed);
    public static int seed
        get { return m_LastSeed; }
        set { m_LastSeed = value; m_Rnd = new Random(m_LastSeed); }
    public static double value
        get { return m_Rnd.NextDouble(); }
    public static double Range(double aMin, double aMax)
        return aMin + m_Rnd.NextDouble() * (aMax - aMin);

This is a C# class. To use it from UnityScript you have to place it either in a folder called “plugins” or “Standard Assets”.

You can use it like Unity’s own Random class:

var someVal = DRandom.Range(-555.123d, 777.456d);

Just like Unity’s Random class it also has a “value” property which just returns a double value between 0.0 and 1.0

var someVal = DRandom.value;

It also has a “seed” property. Since the System.Random class doesn’t update it’s seed and doesn’t provide any access to the internal state of the random generator, the “seed” property just returns the seed which has been used to initialize the random generator. You can set the seed to any “int” value you like to produce always the same sequence of pseudo-random numbers. If no seed is set, the class uses the current time to calculate a seed.

Note: The values generated are not compatible with Unity’s random number generator. So using the same seed in Unity’s Random class won’t yield the same values as this class.