Weighted Random number about a Center
#1
This is from James D Jarvis, a handy way to make random numbers centered and dense around a center point andtapering off within a range. Here my test code I made for this, one for Integers and one for floats, single is assumed Type.

CW stands for Center Weight:
Code: (Select All)
_Title "rndCWI function" 'b+ 2023-01-20
Dim As Long low, high
high = 5
low = -high
Dim As Long a(low - 1 To high + 1)
For i = 1 To 100000
    r = rndCWI(0, high)
    a(r) = a(r) + 1
Next
For i = low - 1 To high + 1
    Print String$(Int(a(i) / 1000 + .5), "*"), a(i) / 1000, i
Next

' 2023-01-20
Function rndCWI (center, range) 'center +/-range  weights to center
    Dim As Long halfRange, c
    halfRange = Int(range) + 1 'for INT(Rnd)  round range in case not integer
    c = Int(center + .5)
    rndCWI = c + Int(Rnd * (halfRange)) - Int(Rnd * (halfRange))
End Function

' 2023-01-20
Function rndCW (C As Single, range As Single) 'center +/-range weights to center
    rndCW = C + Rnd * range - Rnd * range
End Function

Just drop the I from rndCWI to test the float version.
b = b + ...
Reply


Messages In This Thread
Weighted Random number about a Center - by bplus - 01-20-2023, 05:38 PM



Users browsing this thread: 1 Guest(s)