Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Fisher Yates Shuffle for cards or any number of items bplus 4 1,269 06-21-2023, 02:11 PM
Last Post: bplus
  Drop down (& up) number pad OldMoses 1 803 09-24-2022, 03:37 PM
Last Post: mnrvovrfc

Forum Jump:


Users browsing this thread: 1 Guest(s)