Board Logo
« randome numbers in given range »

Welcome Guest. Please Login or Register.
Oct 17th, 2017, 3:28pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: randome numbers in given range  (Read 130 times)
zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 251
xx randome numbers in given range
« Thread started on: Oct 4th, 2017, 4:12pm »

hi,
here is a function to generate random numbers in a given range without repitions
Code:
print rnd1$(20,39)

function rnd1$(min,max)
max=max+1
st$="":n=1
maxlen=max-min
while 1
k=int(rnd(1)*max)
if k<min then kk=min else kk=0
k=k+kk
for j=1 to n
if word$(st$,j)=str$(k) then  exit for
next
if j>n and k<max then st$=st$+str$(k)+chr$(32):n=n+1
if n>maxlen then exit while
wend
rnd1$=st$
end function
 
User IP Logged

AltBas
Full Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 461
xx Re: randome numbers in given range
« Reply #1 on: Oct 5th, 2017, 09:32am »

Fun stuff... Here's an alternate version that handles some special cases.
Code:
dim RN(0)

  Cnt = 13
  call RandList  Cnt, 272, 314
  for a = 1 to Cnt
    print RN(a)
  next a
  print

  Cnt = 15
  call RandList  Cnt, -10, 10
  for a = 1 to Cnt
    print RN(a)
  next a
  print

  Cnt = 6
  call RandList  Cnt, 10, 15
  for a = 1 to Cnt
    print RN(a)
  next a
  print

  Cnt = 4
  call RandList  Cnt, -10, -15
  for a = 1 to Cnt
    print RN(a)
  next a
  print

  Cnt = 7
  call RandList  Cnt, 10, 15
  for a = 1 to Cnt
    print RN(a)
  next a
END

SUB RandList  nRands, Min, Max
  if Max < Min then t = Min : Min = Max : Max = t
  redim RN(nRands)
  dif = Max - Min
  if dif < nRands - 1 then print "Impossible, I say!" : END
  RN(1) = int(rnd(1) * dif) + Min
  for a = 2 to nRands
    DO
      t = int(rnd(1) * dif + 0.5) + Min
      for b = 1 to a
        if RN(b) = t then t = Min - 1 : exit for
      next b
    LOOP until t >= Min
    RN(a) = t
  next a
END SUB
 


- AltBas
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1180
xx Re: randome numbers in given range
« Reply #2 on: Oct 5th, 2017, 11:03am »

Indeed fun stuff!

Many have had so much fun with rand function, they have whittled it down to one-liner in function wrapper.
User IP Logged

B+
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1180
xx Re: randome numbers in given range
« Reply #3 on: Oct 6th, 2017, 11:48am »

;-)) "This is fun stuff!"

To work as intended by author check menu Setup>Prferences
Main Window Columns[80] Rows[25] minimum!
Anything larger than 80 Columns and 25 Rows is fine.

Code:
' secret message from outerspace.txt for JB still v1.01 (B+=MGA) 2017-10-06
' it looks like some entity is griping about JB v2.0 not being out yet

while 1
    scan : txt$ = ""
    while len(txt$) < 1400
        scan
        wlen = rand(1, 12) : wd$ = ""
        while len(wd$) < wlen
            scan : wd$ = wd$;chr$(rand(33, 126))
        wend
        txt$ = txt$;"   ";wd$
    wend
    locate 1, 1 : print: print space$(5);
    for i = 1 to 1400
        scan : print mid$(txt$, i, 1);
        if i mod 70 = 0 then print: print space$(5);
    next
    locate 11, 23
    input "Got all that? because there is a little more, press enter...";temp$
wend
function rand(lo, hi)
    rand = int((hi - lo + 1) * rnd(0)) + lo
end function
 
« Last Edit: Oct 6th, 2017, 11:57am by bplus » User IP Logged

B+
zzz000abc
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 251
xx Re: randome numbers in given range
« Reply #4 on: Oct 7th, 2017, 08:31am »

Thank you AltBas and B+ for presenting better solutions.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls