I am using the Gauss function from my PCG32II code but use Rnd instead of pcg. There are a few Gauss/Normal distribution estimations and the one that I use can be found in many places on the internet.
Gauss is used in 'Mean ± Gauss * Standard_Deviation
From areas of the standardized normal distribution tables we have.
80% will be within 'mean ± 1.28155 * standard_deviation
90% will be within 'mean ± 1.64485 * standard_deviation
95% will be within 'mean ± 1.96 * standard_deviation
99% will be within 'mean ± 2.57583 * standard_deviation
With mean = 100 and standard_deviation = 5 we can expect
80% will be within 100 ± 6.41
90% will be within 100 ± 8.22
95% will be within 100 ± 9.8
99% will be within 100 ± 12.9
Here we request 20 million and segregate them into four bands.
Code: Select all
#Include "string.bi"
#define Perturbation (2*(Rnd<0.5)+1)*Gauss*sd
Function Gauss As Double
Static As Long u2_cached
Static As Double u1, u2, x1, x2, w
If u2_cached = -1 Then
u2_cached = 0
Function = u2
Else
Do
x1 = Rnd
x2 = Rnd
w = x1 * x1 + x2 * x2
Loop While w >= 1
w = Sqr( -2 * Log(w)/w )
u1 = x1 * w : u2 = x2 * w
u2_cached = -1
Function = u1
End If
End Function
Dim As Ulong i, a(0 to 3)
Dim As Double x, mean, sd
Mean = 100 : sd = 5
For i = 1 To 20000000
x = Mean + Perturbation
If x > 100 - 6.41 And x < 100 + 6.41 Then a(0) += 1
If x > 100 - 8.22 And x < 100 + 8.22 Then a(1) += 1
If x > 100 - 9.8 And x < 100 + 9.8 Then a(2) += 1
If x > 100 - 12.9 And x < 100 + 12.9 Then a(3) += 1
Next
Print Format(a(0)/200000, "##.00");" ";Format(a(1)/200000, "##.00");" "; _
Format(a(2)/200000, "##.00");" ";Format(a(3)/200000, "##.00")
Sleep
With one run I get
Code: Select all
80.02 89.98 95.00 99.02
We can use Gauss in a one-sided way where Mean is treated as a minimum value with the return values being less likely the greater they are and as a maximum value with the return values being less likely the smaller they are.
In the following, we sort the return values and note the 'clustering' close to the minimum and maximum values. The last example uses the two-sided approach.
Code: Select all
#define Perturbation (2*(Rnd<0.5)+1)*Gauss*sd
Function Gauss As Double
Static As Long u2_cached
Static As Double u1, u2, x1, x2, w
If u2_cached = -1 Then
u2_cached = 0
Function = u2
Else
Do
x1 = Rnd
x2 = Rnd
w = x1 * x1 + x2 * x2
Loop While w >= 1
w = Sqr( -2 * Log(w)/w )
u1 = x1 * w : u2 = x2 * w
u2_cached = -1
Function = u1
End If
End Function
Sub QuickSort( a() As Double, lo As long, hi As long)
Dim As Long i, j
Dim as Double pivot
If lo < hi Then
i = lo : j = hi
pivot = a( (lo+hi)/2 )
Do
While a(i) < pivot And i < hi: i += 1: Wend
While a(j) > Pivot And j > lo: j -= 1: Wend
If i <= j Then swap a(i), a(j): i += 1: j -= 1
Loop While i <= j
If lo < j Then QuickSort( a(), lo, j )
If hi > i Then QuickSort( a(), i, hi )
End If
End Sub
Dim as Ulong i
Dim as Double Mean, sd, d(1 to 20)
Randomize
Mean = 100 : sd = 2
' Mean as minimum value
For i = 1 to 20
d(i) = Mean + Gauss*sd ' Note +
Next
QuickSort(d(), 1, 20)
For i = 1 to 20
Print d(i)
Next
print
' Mean as maximum value
For i = 1 to 20
d(i) = Mean - Gauss*sd ' Note -
Next
QuickSort(d(), 1, 20)
For i = 1 to 20
print d(i)
Next
Print
' Mean as mean
For i = 1 to 20
d(i) = Mean + Perturbation ' Note ± via Perturbation
Next
QuickSort(d(), 1, 20)
For i = 1 to 20
Print d(i)
Next
Sleep
My games imagination and graphics expertise are woefully lacking but I think that Gauss could be used in games. The first one that occurred to me was dodicat's snooker game; which I cannot find at the moment. The computer thrashed me with every game. When the computer determines a move we could introduce a perturbation. Small perturbations would still see the computer 'putting balls away' all be they slightly 'off centre'. Every now and again a perturbation would be large enough for the computer to 'miss'. Needless to say, we would need to experiment with the standard deviation so as not to encumber the computer too much. Perhaps the standard deviation could be an application variable.
I am sure you games writers could come up with more ideas than me.