public GameObject Generate() { // Create objects grouped by "Generated Objects" GameObject. GameObject parent_object = new GameObject("Generated Objects"); // Create the object under same parent as this script. parent_object.transform.parent = gameObject.transform.parent; // Create the objects with randomized position and scale. for (int i = 0; i <= count; ++i) { GameObject instantiated_obj = Instantiate(object_to_instantiate) as GameObject; //instantiated_obj.transform.position = new Vector3(Random.Range(-pos_range, pos_range), // Random.Range(-pos_range, pos_range), // Random.Range(-pos_range, pos_range)); instantiated_obj.transform.position = new Vector3(RandomFromDistribution.RandomRangeNormalDistribution(-pos_range, pos_range, RandomFromDistribution.ConfidenceLevel_e._999), 0, RandomFromDistribution.RandomRangeNormalDistribution(-pos_range, pos_range, RandomFromDistribution.ConfidenceLevel_e._999)); float scale; switch (size_range_type) { case SizeRangeType_e.Uniform: scale = Random.Range(min_size, max_size); break; case SizeRangeType_e.LinearRight: scale = RandomFromDistribution.RandomRangeLinear(min_size, max_size, 1.0f); break; case SizeRangeType_e.LinearLeft: scale = RandomFromDistribution.RandomRangeLinear(min_size, max_size, -1.0f); break; case SizeRangeType_e.Normal: scale = RandomFromDistribution.RandomRangeNormalDistribution(min_size, max_size, RandomFromDistribution.ConfidenceLevel_e._999); break; case SizeRangeType_e.CurveRight: scale = RandomFromDistribution.RandomRangeSlope(min_size, max_size, 10.0f, RandomFromDistribution.Direction_e.Right); break; case SizeRangeType_e.CurveLeft: scale = RandomFromDistribution.RandomRangeSlope(min_size, max_size, 10.0f, RandomFromDistribution.Direction_e.Left); break; default: scale = Random.Range(min_size, max_size); break; } instantiated_obj.transform.localScale = new Vector3(scale, scale, scale); instantiated_obj.transform.parent = parent_object.transform; } return(parent_object); }
// Use this for initialization void Start() { parentScaleX = parentObject.transform.localScale.x; parentScaleY = parentObject.transform.localScale.y; parentScaleZ = parentObject.transform.localScale.z; gameObject.transform.position = new Vector3(0.0f, -parentScaleY / 2 - transform.localScale.y, 0.0f); float randomX = (float)((int)(RandomFromDistribution.RandomRangeNormalDistribution(-parentScaleX / 2, parentScaleX / 2, RandomFromDistribution.ConfidenceLevel_e._90))); float randomZ = (float)((int)(RandomFromDistribution.RandomRangeNormalDistribution(-parentScaleZ / 2, parentScaleZ / 2, RandomFromDistribution.ConfidenceLevel_e._90))); if (randomX > 0) { randomX -= 0.5f; } else { randomX += 0.5f; } if (randomZ > 0) { randomZ -= 0.5f; } else { randomZ += 0.5f; } gameObject.transform.position += new Vector3(randomX, 0, randomZ); gameObject.transform.parent = parentObject.transform; }
private void GenerateCone(int color) { for (int i = 0; i < 25; i++) { float minAngle = this.transform.rotation.eulerAngles.z - coneAngle; float maxAngle = this.transform.rotation.eulerAngles.z + coneAngle; //Debug.Log("Angulo: " + this.transform.rotation.eulerAngles + " Min: " + minAngle + " Max: " + maxAngle); float newAngle = RandomFromDistribution.RandomRangeNormalDistribution(minAngle, maxAngle, RandomFromDistribution.ConfidenceLevel_e._95); if (newAngle <= 360 && newAngle >= 275) { newAngle = 0; } else if (newAngle >= 180 && newAngle < 275) { newAngle = 180; } newAngle = newAngle * Mathf.Deg2Rad; float newDistance = RandomFromDistribution.RandomRangeExponential(0, distance, 1, RandomFromDistribution.Direction_e.Right); float catetoX = Mathf.Cos(newAngle) * newDistance; float catetoY = Mathf.Abs(Mathf.Sin(newAngle) * newDistance); //Debug.Log("NewAngle: " + newAngle + " NewDistance: " + newDistance + " CatetoX: " + catetoX + " CatetoY: " + catetoY); Vector3 newPoint = this.transform.position + new Vector3(catetoX, catetoY, 0); int shift = Random.Range(0, 2); Instantiate(stain[color * 2 + shift], newPoint, Quaternion.identity); } }
public void MutateNormal() { for (int i = 0; i < genes.Count; i++) { genes[i] += RandomFromDistribution.RandomRangeNormalDistribution(-.125f, .125f, RandomFromDistribution.ConfidenceLevel_e._999); genes[i] = Mathf.Clamp(genes[i], 0f, 1f); } }
float getNextShootAngle() { float rndFloat = RandomFromDistribution.RandomRangeNormalDistribution(0, 1, conf_level); float nextAngle = angle * rndFloat - angle / 2; //Debug.Log(nextAngle); return(nextAngle); }
public static Chromosome GenerateRandomNormalChromosome() { var Dummy = new Chromosome(); for (int i = 0; i < GeneticAlgorithm.chromosomeSize; i++) { Dummy.genes.Add(RandomFromDistribution.RandomRangeNormalDistribution(0, 1, RandomFromDistribution.ConfidenceLevel_e._999)); } return(Dummy); }
//How long it took for Action() to be completed //Action() must first be completed, otherwise an exception will be thrown public int TimeTaken() { if (!actionCompleted) { throw new System.Exception(actionCalled ? "Although Action() has been called, it has not been completed" : "Action() must first be completed"); } float variation = RandomFromDistribution.RandomRangeNormalDistribution( -3 * ExpectedTimeStdDev, 3 * ExpectedTimeStdDev, RandomFromDistribution.ConfidenceLevel_e._998); return(ExpectedTime + (int)variation); }
void generateMaze(float x_min, float x_max, float y_min, float y_max, int lastRandom) { float x_length = x_max - x_min; float y_length = y_max - y_min; if (x_length <= 1 || y_length <= 1) // return if the area is too small to create a new wall. { return; } else { int random1; // 0 = horizontal wall. 1 = vertical wall. Value alternates on recursive calls. if (lastRandom == 0) { random1 = 1; } else if (lastRandom > 0) { random1 = 0; } else { random1 = (int)(Random.value * 2); // 0 or 1. } float random2; if (random1 == 0) // horizontal wall // random using normal distribution makes it more likely that the wall will be placed near the center of the given area. { random2 = (float)((int)(RandomFromDistribution.RandomRangeNormalDistribution(y_min + 1, y_min + y_length - 1, RandomFromDistribution.ConfidenceLevel_e._95))); float x_center = (x_min + x_max) / 2; // x coordinate of the center of the area createWallHorizontal(x_length, x_center, random2); // create wall // recursively call function for the area on either side of the newly created wall generateMaze(x_min, x_max, y_min, random2, random1); generateMaze(x_min, x_max, random2, y_max, random1); } else // vertical wall // random using normal distribution makes it more likely that the wall will be placed near the center of the given area. { random2 = (float)((int)(RandomFromDistribution.RandomRangeNormalDistribution(x_min + 1, x_min + x_length - 1, RandomFromDistribution.ConfidenceLevel_e._95))); float y_center = (y_min + y_max) / 2; // y coordinate of the center of the area createWallVertical(y_length, random2, y_center); // create wall // recursively call function for the area on either side of the newly created wall generateMaze(x_min, random2, y_min, y_max, random1); generateMaze(random2, x_max, y_min, y_max, random1); } } return; }
// Update is called once per frame void Update() { if (currNumMonsters < maxNumMonsters) { int index = RandomFromDistribution.RandomChoiceFollowingDistribution(frequencies); GameObject monster = Instantiate(monsters[index]) as GameObject; currNumMonsters++; // Randomize position float x = RandomFromDistribution.RandomRangeNormalDistribution(-5, 5, RandomFromDistribution.ConfidenceLevel_e._90); float y = RandomFromDistribution.RandomRangeNormalDistribution(-5, 5, RandomFromDistribution.ConfidenceLevel_e._90); monster.transform.position = new Vector3(x, y, 0); } }
IEnumerator landRoutine() { if (!collision && !fuelEnd) { gameObject.transform.GetChild(0).GetComponent <SpriteRenderer>().sprite = planeLanding; } console.text = "Preparing Landing" + "\n"; yield return(new WaitForSeconds(RandomFromDistribution.RandomRangeNormalDistribution(1, 3, conf_level))); console.text = console.text + "\n" + "Seatbells - CHECK"; yield return(new WaitForSeconds(RandomFromDistribution.RandomRangeNormalDistribution(1, 3, conf_level))); console.text = console.text + "\n" + "Security - CHECK"; yield return(new WaitForSeconds(RandomFromDistribution.RandomRangeNormalDistribution(1, 3, conf_level))); console.text = console.text + "\n" + "Spoilers - CHECK"; yield return(new WaitForSeconds(RandomFromDistribution.RandomRangeNormalDistribution(1, 3, conf_level))); console.text = console.text + "\n" + "Wheels - CHECK" + "\n"; console.text = console.text + "\n" + "Start Landing"; StartCoroutine(MoveToPosition(new Vector3(0, 0, 0), 5.0f)); }
void CreateCells() { RandomFromDistribution.ConfidenceLevel_e conf_level = RandomFromDistribution.ConfidenceLevel_e._80; int numberOfCells = levelStats.numberOfCells; float roomCircleRadius = levelStats.roomCircleRadius; percFromGraphToPaths = levelStats.percFromGraphToPaths; mainRoomMeanCutoff = levelStats.mainRoomCutoff; float cellMinWidth = levelStats.cellMinWidth; float cellMaxWidth = levelStats.cellMaxWidth; float cellMinHeight = levelStats.cellMinHeight; float cellMaxHeight = levelStats.cellMaxHeight; for (int i = 0; i < numberOfCells; i++) { float minWidthScalar = cellMinWidth; float maxWidthScalar = cellMaxWidth; float minHeightScalar = cellMinHeight; float maxHeightScalar = cellMaxHeight; GeneratorCell cell = new GeneratorCell(); cell.width = Mathf.RoundToInt(RandomFromDistribution.RandomRangeNormalDistribution(minWidthScalar, maxWidthScalar, conf_level)); cell.height = Mathf.RoundToInt(RandomFromDistribution.RandomRangeNormalDistribution(minHeightScalar, maxHeightScalar, conf_level)); Vector2 pos = GetRandomPointInCirlce(roomCircleRadius); cell.posX = Mathf.RoundToInt(pos.x); cell.posY = Mathf.RoundToInt(pos.y); cell.index = i; cells.Add(cell); widthAvg += cell.width; heightAvg += cell.height; } widthAvg /= cells.Count; heightAvg /= cells.Count; }
// Update is called once per frame void Update() { if (Time.time >= nextSpawnTime) { float secondsBetweenSpawn = Mathf.Lerp(secondsBetweenSpawnMinMax.y, secondsBetweenSpawnMinMax.x, DifficultyManager.getCurrentDifficulty()); float spawnSize = Random.Range(spawnSizeMinMax.x, spawnSizeMinMax.y); float spawnAngle = Random.Range(-spawnAngleMax, spawnAngleMax); Vector2 spawnPosition = new Vector2(Random.Range(-screenHalfSize.x, screenHalfSize.x), screenHalfSize.y + spawnSize * fallingRockPrefab.transform.localScale.y); GameObject newRock = (GameObject)Instantiate(fallingRockPrefab, spawnPosition, Quaternion.Euler(Vector3.forward * spawnAngle)); newRock.transform.localScale = Vector2.one * spawnSize; nextSpawnTime = Time.time + secondsBetweenSpawn; } if (Time.time >= nextPowerUpTime) { float spawnAngle = Random.Range(-spawnAngleMax / 2, spawnAngleMax / 2); GameObject powerupPrefab = powerups[Mathf.CeilToInt(Random.Range(0, powerups.Length))]; Vector2 spawnPosition = new Vector2(Random.Range( -screenHalfSize.x + powerupPrefab.transform.localScale.x, screenHalfSize.x - powerupPrefab.transform.localScale.x), screenHalfSize.y + powerupPrefab.transform.localScale.y); Instantiate(powerupPrefab, spawnPosition, Quaternion.Euler(Vector3.forward * spawnAngle)); nextPowerUpTime = Time.time + RandomFromDistribution.RandomRangeNormalDistribution(powerUpDelay.x, powerUpDelay.y, RandomFromDistribution.ConfidenceLevel_e._95); } }
/// <summary> /// Generates the environment for the experiments. /// </summary> private void GenerateEnvironment() { // delete old obstacles if (obstaclesList.Count > 0) { foreach (GameObject obj in obstaclesList) { obj.SetActive(false); Destroy(obj.gameObject); } obstaclesList.Clear(); } // set new track width grid.gridWorldSize = new Vector2(Random.Range(12, 20), grid.gridWorldSize.y); Vector2 gS = grid.gridWorldSize; // place detectable obstacles on the sidelines for (int i = 0; i < sideObstacleCount; i++) { // calculate coordinates float x = RandomFromDistribution.RandomRangeNormalDistribution(-3f, 4f, RandomFromDistribution.ConfidenceLevel_e._99) + gS.x / 2; float z = Mathf.Lerp(-gS.y / 2, gS.y / 2, (float)i / (float)sideObstacleCount); // objects on the right GameObject obsToBuild = loadedObstacles[Random.Range(0, loadedObstacles.Length)]; Vector3 obsSize = obsToBuild.GetComponentInChildren <Renderer>().bounds.size; float maxSize = Mathf.Max(obsSize.x, obsSize.y); Vector3 newPos = new Vector3(x + maxSize / 3, 0f, z); Quaternion newRot = Quaternion.Euler(Random.Range(-5f, 5f), Random.Range(0f, 360f), Random.Range(-5f, 5f)); GameObject newObs = Instantiate(obsToBuild, newPos, newRot) as GameObject; newObs.GetComponentInChildren <MeshRenderer>().material = ObstacleMat; newObs.layer = 8; obstaclesList.Add(newObs); // objects on the left obsToBuild = loadedObstacles[Random.Range(0, loadedObstacles.Length)]; obsSize = obsToBuild.GetComponentInChildren <Renderer>().bounds.size; maxSize = Mathf.Max(obsSize.x, obsSize.y); newPos = new Vector3(-x - maxSize / 3, 0f, z); newRot = Quaternion.Euler(Random.Range(-5f, 5f), Random.Range(0f, 360f), Random.Range(-5f, 5f)); newObs = Instantiate(obsToBuild, newPos, newRot) as GameObject; newObs.GetComponentInChildren <MeshRenderer>().material = ObstacleMat; newObs.layer = 8; obstaclesList.Add(newObs); } // update current obstacle ID if not set to fixed if (!fixedObstacle) { if (currentObstacleID < loadedObstacles.Length - 1) { currentObstacleID++; } else { Debug.Log("Epoch: " + currentEpoch++); currentObstacleID = 0; } } // load central obstacle to avoid GameObject otb = loadedObstacles[currentObstacleID]; Vector3 oS = otb.GetComponentInChildren <Renderer>().bounds.size; // make sure it's not too big float mS = Mathf.Max(oS.x, oS.y); while (mS > grid.gridWorldSize.x / 2f) { otb = loadedObstacles[Random.Range(0, loadedObstacles.Length)]; oS = otb.GetComponentInChildren <Renderer>().bounds.size; mS = Mathf.Max(oS.x, oS.y); } // place it Vector3 nP = new Vector3(Random.value - 0.5f, Random.value - 0.25f, 0f); Quaternion nR = Quaternion.Euler(Random.Range(-5f, 5f), Random.Range(0f, 360f), Random.Range(-5f, 5f)); currentObstacle = Instantiate(otb, nP, nR) as GameObject; obstaclesList.Add(currentObstacle); // make obstacle undetectable in x % of the cases to force crashes if (Random.value < crashChance) { currentObstacle.layer = 9; currentObstacle.GetComponentInChildren <MeshRenderer>().material = CrashObjMat; isSteeringData = false; } else { currentObstacle.layer = 8; currentObstacle.GetComponentInChildren <MeshRenderer>().material = ObstacleMat; isSteeringData = true; } grid.Awake(); }
public override float GetRandomNumber(float min, float max) { return(RandomFromDistribution.RandomRangeNormalDistribution(min, max, conf_level)); }
// Start is called before the first frame update void Start() { nextPowerUpTime = Time.time + RandomFromDistribution.RandomRangeNormalDistribution(powerUpDelay.x, powerUpDelay.y, RandomFromDistribution.ConfidenceLevel_e._95); screenHalfSize = new Vector2(Camera.main.aspect * Camera.main.orthographicSize, Camera.main.orthographicSize); }
void Awake() { int numClusters = Random.Range(numClustersLB, numClustersUB); float xcoord; float ycoord; float zcoord; float magnitude; using (System.IO.StreamWriter file = new System.IO.StreamWriter(Directory.GetCurrentDirectory() + @"\\Assets\\StreamingAssets\\DxRData\\GenPlot.json")) { file.Write("["); for (int i = 0; i < numClusters; ++i) { int pointsInCluster = Random.Range(numPointsLB, numPointsUB); int centerXCoord = Random.Range(XCenterLB, XCenterUB); int centerYCoord = Random.Range(YCenterLB, YCenterUB); int centerZCoord = Random.Range(ZCenterLB, ZCenterUB); if (normalizedDistribution) { xDist = RandomFromDistribution.RandomRangeNormalDistribution(xDistLB, xDist, confLevel); yDist = RandomFromDistribution.RandomRangeNormalDistribution(yDistLB, yDist, confLevel); zDist = RandomFromDistribution.RandomRangeNormalDistribution(zDistLB, zDist, confLevel); } else { xDist = Random.Range(xDistLB, xDist); yDist = Random.Range(yDistLB, yDist); zDist = Random.Range(zDistLB, zDist); } xyRotDeg = RandomFromDistribution.RandomRangeNormalDistribution(0, xyRotDeg, confLevel); yzRotDeg = RandomFromDistribution.RandomRangeNormalDistribution(0, yzRotDeg, confLevel); xzRotDeg = RandomFromDistribution.RandomRangeNormalDistribution(0, xzRotDeg, confLevel); for (int j = 0; j < pointsInCluster; ++j) { if (randomFromDist) { xcoord = RandomFromDistribution.RandomRangeNormalDistribution(-xDist, xDist, confLevel); ycoord = RandomFromDistribution.RandomRangeNormalDistribution(-yDist, yDist, confLevel); zcoord = RandomFromDistribution.RandomRangeNormalDistribution(-zDist, zDist, confLevel); if (isSphere) { magnitude = (float)System.Math.Sqrt(xcoord * xcoord + ycoord * ycoord + zcoord * zcoord); xcoord = xDist * (xcoord / magnitude); ycoord = yDist * (ycoord / magnitude); zcoord = zDist * (zcoord / magnitude); // xy plane rotation xcoord = xcoord * Mathf.Cos(0.0174533f * xyRotDeg) - ycoord * Mathf.Sin(0.0174533f * xyRotDeg); ycoord = xcoord * Mathf.Sin(0.0174533f * xyRotDeg) + ycoord * Mathf.Cos(0.0174533f * xyRotDeg); // yz plane rotation ycoord = ycoord * Mathf.Cos(Mathf.Deg2Rad * yzRotDeg) - zcoord * Mathf.Sin(Mathf.Deg2Rad * yzRotDeg); zcoord = ycoord * Mathf.Sin(Mathf.Deg2Rad * yzRotDeg) + zcoord * Mathf.Cos(Mathf.Deg2Rad * yzRotDeg); // xz rotation xcoord = xcoord * Mathf.Cos(Mathf.Deg2Rad * xzRotDeg) - zcoord * Mathf.Sin(Mathf.Deg2Rad * xzRotDeg); zcoord = xcoord * Mathf.Sin(Mathf.Deg2Rad * xzRotDeg) + zcoord * Mathf.Cos(Mathf.Deg2Rad * xzRotDeg); xcoord += centerXCoord; ycoord += centerYCoord; zcoord += centerZCoord; } else { xcoord += centerXCoord; ycoord += centerYCoord; zcoord += centerZCoord; } } else { xcoord = Random.Range(xDistLB, xDist + 1); ycoord = Random.Range(yDistLB, yDist + 1); zcoord = Random.Range(zDistLB, zDist + 1); if (isSphere) { magnitude = (float)System.Math.Sqrt(xcoord * xcoord + ycoord * ycoord + zcoord * zcoord); xcoord = xDist * (xcoord / magnitude); ycoord = yDist * (ycoord / magnitude); zcoord = zDist * (zcoord / magnitude); // xy plane rotation xcoord = xcoord * Mathf.Cos(0.0174533f * xyRotDeg) - ycoord * Mathf.Sin(0.0174533f * xyRotDeg); ycoord = xcoord * Mathf.Sin(0.0174533f * xyRotDeg) + ycoord * Mathf.Cos(0.0174533f * xyRotDeg); // yz plane rotation //ycoord = ycoord * Mathf.Cos(Mathf.Deg2Rad * yzRotDeg) - zcoord * Mathf.Sin(Mathf.Deg2Rad * yzRotDeg); //zcoord = ycoord * Mathf.Sin(Mathf.Deg2Rad * yzRotDeg) + zcoord * Mathf.Cos(Mathf.Deg2Rad * yzRotDeg); // xz rotation //xcoord = xcoord * Mathf.Cos(Mathf.Deg2Rad * xzRotDeg) - zcoord * Mathf.Sin(Mathf.Deg2Rad * xzRotDeg); //zcoord = xcoord * Mathf.Sin(Mathf.Deg2Rad * xzRotDeg) + zcoord * Mathf.Cos(Mathf.Deg2Rad * xzRotDeg); xcoord += centerXCoord; ycoord += centerYCoord; zcoord += centerZCoord; } else { xcoord += centerXCoord; ycoord += centerYCoord; zcoord += centerZCoord; } } int color = i; // need x, y, z, color file.Write("\n\t{ \n \t\t \"Xcoord\": " + xcoord + ","); file.Write("\n\t\t \"Ycoord\": " + ycoord + ","); file.Write("\n\t\t \"Zcoord\": " + zcoord + ","); file.Write("\n\t\t \"Color\": " + color + "\n\t}"); if (j != pointsInCluster - 1 && i != numClusters - 1) { file.Write(","); } } } file.Write("]"); } }