sequence of random numbers are not repeated

Started by ATLUS, April 03, 2012, 09:06:08 AM

Previous topic - Next topic

ATLUS

 :)

PlayBASIC Code: [Select]
; 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

kevin

#1
 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.


PlayBASIC Code: [Select]
   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





Related To:

Shuffle Deck Of cards


ATLUS

#2
Swap work faster
i used swap in Puzzle 15

PlayBASIC Code: [Select]
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