QB64 Phoenix Edition
Weighted Random number about a Center - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Utilities (https://qb64phoenix.com/forum/forumdisplay.php?fid=8)
+---- Thread: Weighted Random number about a Center (/showthread.php?tid=1410)



Weighted Random number about a Center - bplus - 01-20-2023

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.


RE: Weighted Random number about a Center - James D Jarvis - 01-20-2023

Distribution curves, inspiring math nerds for generations.


RE: Weighted Random number about a Center - dcromley - 01-25-2023

This reminded me of my "Bates distribution" program in my files, which I ported: (Very good in QB64.)

Code: (Select All)
_Title "Bates distribution" ' dcromley
' https://en.wikipedia.org/wiki/Bates_distribution
Option _Explicit
Screen _NewImage(1024, 768, 256)
Color 0, 15
Cls
Dim Shared n000 as Long, yfactor
Dim exp2
n000 = 10 ^ 6 ' big number

For exp2 = 0 To 3 ' for 1,2,4,8
  Dim nrnds
  nrnds = 2 ^ exp2 ' now 1,2,4,8
  yfactor = 768 * 1024 / n000 / 5 ' height
  doDist (nrnds) ' plot a distribution
Next exp2

Locate 37, 62: Print "1 rnd"
Locate 28, 62: Print "2 rnds"
Locate 21, 62: Print "4 rnds"
Locate 10, 62: Print "8 rnds"
While InKey$ = "": Wend: System

Sub doDist (nrnds) ' plot distribution
  Dim i as Long, x, r, a(1023)
  For i = 1 To n000 ' fill array with counts
    r = Int(rand(nrnds) / nrnds * 1024) ' random number 0 to 1023
    a(r) = a(r) + 1 ' count of hits
  Next i
  For x = 0 To 1022 ' plot array
    Line (x, 767 - a(x) * yfactor)-(x + 1, 767 - a(x + 1) * yfactor)
  Next x
End Sub

Function rand (kn) ' sum of kn random numbers 0 to 1
  Dim i, ret
  For i = 1 To kn
    ret = ret + Rnd
  Next i
  rand = ret
End Function



RE: Weighted Random number about a Center - bplus - 01-25-2023

Yeah, that's a nerdy looking thing Smile