/// <summary> /// Returns a random number between -1 and 1 /// /// Note that this is my own crazy stuff adapted for this purpose only and do not necessarily correspond correctly to their names /// in the mathematical sense /// </summary> /// <returns>The random.</returns> /// <param name="randomType">Random type.</param> static float GetRandom(RandomDistributionType randomType) { switch (randomType) { case RandomDistributionType.LINEAR: return(Random.Range(-1f, 1f)); case RandomDistributionType.SQUARE_ROOT: return(Mathf.Sqrt(Random.Range(0f, 0.5f)) * (Random.Range(0f, 1f) > 0.5f ? -1:1)); case RandomDistributionType.GAUSSIAN: float u, v, S; do { u = 2.0f * Random.Range(0f, 1f) - 1.0f; v = 2.0f * Random.Range(0f, 1f) - 1.0f; S = u * u + v * v; }while (S >= 1.0f); float fac = Mathf.Sqrt(-2.0f * Mathf.Log(S) / S); fac = u * fac / 3f; return(Mathf.Clamp(fac, -1f, 1f)); case RandomDistributionType.TWO_RANDS: return(Random.Range(-1f, 1f) * Random.Range(0f, 1f)); case RandomDistributionType.THREE_RANDS: return(Random.Range(-1f, 1f) * Random.Range(0f, 1f) * Random.Range(0f, 1f)); default: return(Random.Range(-1f, 1f)); } }
public static int[,] DiamondSquare(int heightMapSize, RandomDistributionType randomType, float hillyness) { MapGenerator.heightMapSize = heightMapSize; MapGenerator.hillyness = hillyness; MapGenerator.randomType = randomType; // calculate heightmap int[,] heightMap = new int[heightMapSize, heightMapSize]; //First set the four corners of the map if (Random.Range(0f, 1f) < hillyness) { heightMap[0, 0] = GetRandomHeight(-heightMapSize / 4, heightMapSize / 4); } if (Random.Range(0f, 1f) < hillyness) { heightMap[0, heightMapSize - 1] = GetRandomHeight(-heightMapSize / 4, heightMapSize / 4); } if (Random.Range(0f, 1f) < hillyness) { heightMap[heightMapSize - 1, 0] = GetRandomHeight(-heightMapSize / 4, heightMapSize / 4); } if (Random.Range(0f, 1f) < hillyness) { heightMap[heightMapSize - 1, heightMapSize - 1] = GetRandomHeight(-heightMapSize / 4, heightMapSize / 4); } for (int halfStep = (heightMapSize - 1) / 2; halfStep > 0; halfStep /= 2) { for (int x = halfStep; x < heightMapSize - halfStep; x += 2 * halfStep) { for (int y = halfStep; y < heightMapSize - halfStep; y += 2 * halfStep) { DiamondStep(heightMap, halfStep, x, y); } } } return(heightMap); }
/// <summary> /// /// </summary> /// <param name="distrubutionType"></param> /// <param name="seed"></param> /// <param name="m"></param> /// <param name="n"></param> public NRealMatrix RandomMatrix(RandomDistributionType distrubutionType, MCJIMatrix seed, int m, int n) { return(new NRealMatrix(_lib.randomMatrix(Convert.ToUInt16(distrubutionType), seed, m, n))); }
/// <summary> /// /// </summary> /// <param name="distrubutionType"></param> /// <param name="seed"></param> /// <param name="m"></param> /// <param name="n"></param> public NRealMatrix RandomMatrix(RandomDistributionType distrubutionType, MCJIMatrix seed, int m,int n) { return new NRealMatrix(_lib.randomMatrix(Convert.ToUInt16(distrubutionType), seed, m, n)); }