UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: ATLUS on April 03, 2012, 09:06:08 AM

Title: sequence of random numbers are not repeated
Post by: ATLUS on April 03, 2012, 09:06:08 AM
 :)

[pbcode]; PROJECT : Project1
; AUTHOR  : ATLUS
; CREATED : 02.04.2012
; EDITED  : 03.04.2012
; ---------------------------------------------------------------------
loadfont "Times New Romans",2,24,1
setfont 2
dim x(10)
flag=0
dim tx(10)
y=0
x(1)=10

For lp=2 To 10
y=RndRange(1,10)
      for i=1 to 10
         if y=x(i)
            flag=0
          endif
   next i
      
while flag=0
      y=RndRange(1,10)
      flag=1
   for i=1 to 10
      if y=x(i)
         flag=0   
endif
next i   

endwhile
   
x(lp)=y
flag=1
Next lp


for k=1 to 10
print x(k)
next k

Sync
waitkey[/pbcode]
Title: Re: sequence of random numbers are not repeated
Post by: kevin on April 03, 2012, 09:16:14 AM
 Well, it seems to work ok.. But, there's an easier way of doing it.    We just fill a table of all the possible values/items, then swap them to shuffle.


[pbcode]
  Size=20
    Dim Values(Size)

Do
   cls


    ; init table with all the possible values
     For lp =0 to Size
        Values(lp)=lp
     next

    ; perform swaps
      For lp =0 to Size
         ; pick a value, while avoiding float->integer rounding
         Index=floor(Rnd#(Size))

         ; swap current item for whatever item was selected
         Temp=Values(lp)
         Values(lp)=Values(Index)
         Values(Index)=Temp
      next

   ; display the result
    For lp =0 to Size
         print Values(lp)
    next


Sync
waitkey
loop

[/pbcode]


Related To:

 Shuffle Deck Of cards (http://www.underwaredesign.com/forums/index.php?topic=3485.0)

Title: Re: sequence of random numbers are not repeated
Post by: ATLUS on April 03, 2012, 09:39:13 AM
Swap work faster
i used swap in Puzzle 15

[pbcode]for r=0 to 16
   w=rndrange(1,16)
   g=rndrange(1,16)
   tempx = puz(w).xpos
   tempy = puz(w).ypos
   tempnumber  = puz(w).number
               
   puz(w).xpos = puz(g).xpos
   puz(w).ypos = puz(g).ypos
   puz(w).number = puz(g).number
               
   puz(g).xpos = tempx
   puz(g).ypos = tempy
   puz(g).number = tempnumber
next r[/pbcode]