- Home /
This question was
closed Sep 22, 2017 at 08:18 PM by
hexagonius for the following reason:
A gaussian generator has nothing to do with Unity in particular. Some other forum concerned about mathematical algorithms would be more suitable
Random Gaussian Generator
Hello, everyone
I want to create a random Gaussian generator that will take two arguments a "mean" value and a "standard deviation". I have found some solutions across the web and test them. Although the results seem good, I am creating a simulator and I must be sure about the values that are returned.
pseudocode :
RandomGaussianGenerator(flaot mean , sigma)
The script that Iam using is this :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomGaussianGenerator : MonoBehaviour {
private void Start()
{
for (int i = 0; i < 20; i++)
{
print(GenerateNormalRandom(10.0f, 1.67f,5,15));
}
}
public static float GenerateNormalRandom(float mean, float sigma,int min,int max)
{
float rand1 = Random.Range(0.0f, 1.0f);
float rand2 = Random.Range(0.0f, 1.0f);
float n = Mathf.Sqrt(-2.0f * Mathf.Log(rand1)) * Mathf.Cos((2.0f * Mathf.PI) * rand2);
int generatedNumber = Mathf.FloorToInt (mean + sigma * n);
generatedNumber = Mathf.Clamp(generatedNumber, min, max);
return generatedNumber;
}
}
If anyone can suggest something more accurate or tested it will be greatly appreciated.
Comment
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why is my ID value getting stuck here? 1 Answer