Weekly Qbasic and Qb64 Lesson Topics
April 23, 2018, 04:30:07 pm
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Want to see a specific Tutorial? ASK!
 
  Home Help Search Arcade Links Staff List Login Register  

RndRange() -- one of my all-time favorite routines


Pages: [1]
  Print  
Author Topic: RndRange() -- one of my all-time favorite routines  (Read 250 times)
Quark
Post Demos
*
Posts: 14


« on: June 15, 2011, 10:02:51 am »

I use this in many programs I write, so handy:

Function RndRange%(lo As Integer,hi As Integer)
  ' Returns a number within a range, e.g. RndRange(1,6) gives 1 or 2 or 3 or 4 or 5 or 6
  Return INT(RND*(hi-lo+1)+lo)
End Function

--Quark
Report Spam   Logged

Social Buttons

Quark
Post Demos
*
Posts: 14


« Reply #1 on: June 15, 2011, 10:42:49 pm »

OldDosLover suggested that RndRange could use an example.  I could do a standard dice throw using two dice: Print "You have rolled the dice and come up with " +str$(RndRange(1,6)) + "and " str$(RndRange(1,6)).

However (heh), I just finished a little self-learning exercise that was fun for me, who isn't much of a graphics person and it uses RndRange a lot.  Have a look at:

' Trapped square
SCREEN _NEWIMAGE(640, 480, 256)
DIM w, h, x1, y1, x2, y2, dx, dy AS INTEGER
w = _WIDTH(0): h = _HEIGHT(0)
x1 = w / 2 - 50: y1 = h / 2 - 50
x2 = x1 + 100: y2 = y1 + 100: dx = 1: dy = 1
RANDOMIZE TIMER
DO
  LINE (x1, y1)-(x2, y2), _RGB(&H00, &H00, &H00), BF
  IF x1 < 1 THEN dx = RndRange(1, 3)
  IF y1 < 1 THEN dy = RndRange(1, 3)
  IF x2 >= w - 1 THEN dx = -RndRange(1, 3)
  IF y2 >= h - 1 THEN dy = -RndRange(1, 3)
  x1 = x1 + dx: y1 = y1 + dy: x2 = x2 + dx: y2 = y2 + dy
  LINE (x1, y1)-(x2, y2), _RGB(&HFF, &H40, &H40), BF
  _DELAY (.005)
  IF INKEY$ <> "" THEN EXIT DO
LOOP
END
FUNCTION RndRange% (lo AS INTEGER, hi AS INTEGER)
RndRange = INT(RND * (hi - lo + 1) + lo)
END FUNCTION

This reminded me of the old Qix game somehow...

--Quark
Report Spam   Logged
OlDosLover
Guest
« Reply #2 on: June 15, 2011, 10:59:10 pm »

Hi all,
    A fine example of a really good and useful function. I took the liberty of rewriting it slightly. Hope you like my version.
Code:
' Trapped square
DEFINT A-Z

SCREEN _NEWIMAGE(640, 480, 256)

DIM w, h, x1, y1, x2, y2, dx, dy AS INTEGER

w = _WIDTH(0): h = _HEIGHT(0)
x1 = w / 2 - 50: y1 = h / 2 - 50
x2 = x1 + 100: y2 = y1 + 100: dx = 1: dy = 1
RANDOMIZE TIMER

DO
  _LIMIT 100
  LINE (x1, y1)-(x2, y2), _RGB(&H00, &H00, &H00), BF
  IF x1 < 1 THEN dx = RndRange(1, 3)
  IF y1 < 1 THEN dy = RndRange(1, 3)
  IF x2 >= w - 1 THEN dx = -RndRange(1, 3)
  IF y2 >= h - 1 THEN dy = -RndRange(1, 3)
  x1 = x1 + dx: y1 = y1 + dy: x2 = x2 + dx: y2 = y2 + dy
  LINE (x1, y1)-(x2, y2), _RGB(&HFF, &H40, &H40), BF
  _DISPLAY
LOOP WHILE INKEY$ = ""
END
FUNCTION RndRange% (lo AS INTEGER, hi AS INTEGER)
RndRange = INT(RND * (hi - lo + 1) + lo)
END FUNCTION
OlDosLover.
Report Spam   Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by EzPortal
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum

Buy traffic for your forum/website
traffic-masters
Powered by SMF | SMF © 2016, Simple Machines