Author Topic: RandomWeight  (Read 3190 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
RandomWeight
« on: March 19, 2019, 07:20:42 pm »
Say you want a random number in a range between 2 values and you want one end of that range to be returned many more times than from the other end. Call the value on the many side the manyValue and the value on the few side the fewValue and power to increase the weight to the manyValue side. randWeight is a simple little function that will do this as tested and demo'd here:
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 720, 32)
  2. _SCREENMOVE 100, 20
  3. minN = 40
  4. maxN = 10
  5. IF minN > maxN THEN
  6.     fini = minN: start = maxN
  7.     fini = maxN: start = minN
  8. DIM c(start TO fini)
  9. FOR i = 1 TO 10000
  10.     t = randWeight(maxN, minN, 2)
  11.     'PRINT t; ", ";
  12.     c(t) = c(t) + 1
  13. PRINT "For 10000 tests here is the frequency distribution for randWeight("; maxN; ","; minN; ", 2) "
  14. FOR i = start TO fini
  15.     PRINT i, c(i)
  16. PRINT "Press any to see graphic with boxes: width weighted on short side, height on high side."
  17. PRINT " So that generally the boxes will be mostly longer than wide."
  18.  
  19. FOR p = 1 TO 5
  20.     cnt = 0
  21.     CLS
  22.     FOR i = 1 TO 100
  23.         w = randWeight(maxN, minN, p): h = randWeight(minN, maxN, p) '<<<<<<<< see what happens as you play the power parameter
  24.         IF w > h THEN
  25.             c~& = &HFFFF0000
  26.         ELSE
  27.             c~& = &HFF0000FF: cnt = cnt + 1
  28.         END IF
  29.         LINE (780 * RND, 690 * RND)-STEP(w, h), c~&, B
  30.     NEXT
  31.     PRINT "For power ="; p; " there are:"; cnt; " blue boxes out of 100 that are longer than wide."
  32.     _DELAY 3
  33.  
  34. PRINT "another Demo press any to cont..."
  35. horizon = .35 * _HEIGHT
  36. FOR i = 0 TO horizon
  37.     LINE (0, i)-(_WIDTH, i), _RGB(0, 0, .25 * i + 100)
  38. FOR i = horizon TO _HEIGHT
  39.     LINE (0, i)-(_WIDTH, i), _RGB(0, 255 - .25 * i - 50, 0)
  40. FOR i = 1 TO 1000
  41.     y = randWeight(horizon, _HEIGHT, 5)
  42.     CIRCLE (_WIDTH * RND, y), y * .003, _RGB(255, 255, 0)
  43.  
  44.  
  45. FUNCTION randWeight (manyValue, fewValue, power)
  46.     randWeight = manyValue + RND ^ power * (fewValue - manyValue)
  47.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: RandomWeight
« Reply #1 on: March 19, 2019, 07:27:27 pm »
And here is Matrix Rain #4 using randWeight to assign many smaller size drops with fewer large drops for better 3d effect plus an improvement in transparent background of letters that I don't think my last version of Matrix Rain had... whatever, I like the look much better than Matrix Rain #3.

Code: QB64: [Select]
  1. _TITLE "Matrix Rain 4" 'B+ started 2019-03-16
  2. ' from Matrix Rain 2019-03-14
  3. ' or QB64 Purple Rain!
  4.  
  5. '>>> Save this file as: Matrix Rain 4.bas, so the program can load the strings from it.  <<<
  6.  
  7. '2019-03-15 This will attempt to spin the drops as they fall
  8. '2019-03-16  Don't need no damn Character Set.DAT file!!!
  9. '2019-03-16 Ijust want to see the vertical code strings dangle and twist.
  10.  
  11. '2019-03-19 Matrix Rain 4
  12. ' + added randWeight to weight the random sizes chosen so many more on small side than large
  13. ' + draw letters on a transparent background so the background of the letter does not cover
  14. '   the drops behind it.
  15.  
  16.  
  17. CONST xmax = 1280
  18. CONST ymax = 740
  19. CONST nDrops = 500
  20. TYPE dropType
  21.     x AS SINGLE
  22.     sz AS SINGLE
  23.     curY AS INTEGER
  24.     dxs AS SINGLE 'direction and change of spin some small fraction of 1, +-1/3, +-1/4, +-1/5...
  25.  
  26. SCREEN _NEWIMAGE(xmax, ymax, 32)
  27. _SCREENMOVE 80, 0 'for snap shot
  28. '_FULLSCREEN 'as screen saver
  29.  
  30. REDIM SHARED fileStrings$(1000) 'container for these program lines that will be dangling
  31. OPEN "Matrix Rain 4.bas" FOR INPUT AS #1
  32.     LINE INPUT #1, fs$
  33.     IF LEN(LTRIM$(fs$)) <> 0 THEN 'less empty spaces
  34.         fileStrings$(i) = LTRIM$(fs$)
  35.         i = i + 1
  36.     END IF
  37. REDIM _PRESERVE fileStrings$(i - 1)
  38. ' check loading
  39. 'FOR i = 0 TO UBOUND(fileStrings$)
  40. '    PRINT i, fileStrings$(i)
  41. 'NEXT
  42. 'END
  43.  
  44. 'setup drops
  45. DIM SHARED drop(nDrops) AS dropType
  46. DIM SHARED s$(nDrops)
  47.  
  48. FOR i = 0 TO nDrops
  49.     newDrop i, 1
  50.  
  51. WHILE _KEYDOWN(27) = 0
  52.     CLS
  53.     FOR i = 0 TO nDrops
  54.         drawDrop (i)
  55.         drop(i).curY = drop(i).curY + 1
  56.         IF drop(i).curY > LEN(s$(i)) THEN newDrop i, 0
  57.     NEXT
  58.     _DISPLAY
  59.     _LIMIT 25
  60.  
  61. SUB newDrop (i, start)
  62.     drop(i).x = RND * xmax 'set location
  63.     drop(i).sz = randWeight(.3, 5, 3) 'set size  weighted on small sizes
  64.     'length of text string can fit on screen
  65.     charLength = ymax \ (drop(i).sz * 16) + 1 'from size determine how many chars fit on screen
  66.     randLine = INT(RND * UBOUND(fileStrings$)) 'pick a random program line
  67.     s$(i) = MID$(fileStrings$(randLine), 1, charLength) 'here is text string to dangle
  68.     WHILE LEN(s$(i)) < charLength
  69.         IF randLine + 1 > UBOUND(fileStrings$) THEN randLine = 0 ELSE randLine = randLine + 1
  70.         s$(i) = MID$(s$(i) + " : " + fileStrings$(randLine), 1, charLength)
  71.     WEND
  72.     IF start <> 0 THEN drop(i).curY = INT(RND * (charLength)) + 1 ELSE drop(i).curY = 1 'flat and readable at curY
  73.     drop(i).dxs = 1 / (INT(RND * 7) + 3) 'change of spin rate +-1/3, +-1/4, ... +-1/9
  74.     IF RND < .5 THEN drop(i).dxs = -drop(i).dxs
  75.  
  76. SUB drawDrop (i)
  77.     FOR j = 1 TO drop(i).curY
  78.         d = drop(i).curY - j
  79.         IF d = 0 THEN
  80.             c~& = _RGBA32(255, 100, 255, 225)
  81.         ELSEIF d = 1 THEN
  82.             c~& = _RGBA32(255, 50, 255, 205)
  83.         ELSEIF d = 2 THEN
  84.             c~& = _RGBA32(255, 25, 255, 180)
  85.         ELSEIF d >= 3 THEN
  86.             c~& = _RGBA32(255, 0, 255, 190 - d * 5)
  87.         END IF
  88.         rot = 1: dir = -1
  89.         FOR k = 0 TO d
  90.             rot = rot + drop(i).dxs * dir
  91.             IF rot > 1 THEN dir = -1 * dir: rot = 1 + drop(i).dxs * dir
  92.             IF rot < -1 THEN dir = dir * -1: rot = -1 + drop(i).dxs * dir
  93.         NEXT
  94.         drwChar MID$(s$(i), j, 1), c~&, drop(i).x + 4 * drop(i).sz, drop(i).sz * 16 * (j - 1) + 8 * drop(i).sz, rot * drop(i).sz, drop(i).sz, 0
  95.     NEXT
  96.  
  97. SUB drwChar (char$, c AS _UNSIGNED LONG, midX, midY, xScale, yScale, Rotation) 'what ever the present color is set at
  98.     I& = _NEWIMAGE(8, 16, 32)
  99.     _DEST I&
  100.     COLOR c, _RGBA32(0, 0, 0, 0)
  101.     _PRINTSTRING (0, 0), char$
  102.     _DEST 0
  103.     RotoZoom2 midX, midY, I&, xScale, yScale, Rotation
  104.     _FREEIMAGE I&
  105.  
  106. SUB RotoZoom2 (X AS LONG, Y AS LONG, Image AS LONG, xScale AS SINGLE, yScale AS SINGLE, Rotation AS SINGLE)
  107.     DIM px(3) AS SINGLE: DIM py(3) AS SINGLE
  108.     W& = _WIDTH(Image&): H& = _HEIGHT(Image&)
  109.     px(0) = -W& / 2: py(0) = -H& / 2: px(1) = -W& / 2: py(1) = H& / 2
  110.     px(2) = W& / 2: py(2) = H& / 2: px(3) = W& / 2: py(3) = -H& / 2
  111.     sinr! = SIN(-Rotation): cosr! = COS(-Rotation)
  112.     FOR i& = 0 TO 3
  113.         x2& = (px(i&) * cosr! + sinr! * py(i&)) * xScale + X: y2& = (py(i&) * cosr! - px(i&) * sinr!) * yScale + Y
  114.         px(i&) = x2&: py(i&) = y2&
  115.     NEXT
  116.     _MAPTRIANGLE (0, 0)-(0, H& - 1)-(W& - 1, H& - 1), Image& TO(px(0), py(0))-(px(1), py(1))-(px(2), py(2))
  117.     _MAPTRIANGLE (0, 0)-(W& - 1, 0)-(W& - 1, H& - 1), Image& TO(px(0), py(0))-(px(3), py(3))-(px(2), py(2))
  118.  
  119. FUNCTION randWeight (manyValue, fewValue, power)
  120.     randWeight = manyValue + RND ^ power * (fewValue - manyValue)
  121.  
  122.  

EDIT: remove attachment tag in the code, I wondered where that went. :D
Matrix Rain 4.PNG
* Matrix Rain 4.PNG (Filesize: 162.28 KB, Dimensions: 1269x749, Views: 180)
« Last Edit: March 19, 2019, 08:21:54 pm by bplus »

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: RandomWeight
« Reply #2 on: March 19, 2019, 07:59:11 pm »
nice bplus :)