Page 1 of 1 1
Topic Options
#165025 - 2006-07-25 02:41 PM Getting random elements from an array until all elements are done
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Hi guys,

I need a little help with this one cause I can’t seem to figure this one out myself.

I have a file that contains 18.490 lines. I read the complete file into an array with the readfile UDF.
The problem is to get all elements out of the array randomly until all elements are done. It's quite simple to get a random element but how to set some kind of counter so that it will never get an element that I already have and how to let it stop when all elements are done? Or is there some kind of way to get the lines from the text file randomly and only get each line once? It should only get each element once and not skip any of them.

Been starring at this for several hours. Maybe it's simple but I just don’t see it.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#165026 - 2006-07-25 02:59 PM Re: Getting random elements from an array until all elements are done
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
One way could be to create another array of the same dimensions. Then when you check your file array, just do something like

If secondArray[indexOfRndLine]=0
; not been used
set secondArray[IndexOfRndLine]=1
else
already used
endif

Top
#165027 - 2006-07-25 03:12 PM Re: Getting random elements from an array until all elements are done
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
Just thinking outloud... there's probably a better approach... but an idea anyway.

untested
Code:

loadfile into $array
while ubound($array)>-1
$x=randomnumber
$item=$array[$x]
;do stuff with $item
redim $newarray[ubound($array-1)
for $c=0 to $x -1
$newarray[$c]=$array[$c]
loop
for $c=$x + 1 to ubound($array)
$newarray[$c-1]=$array[$c]
next
$array=$newarray
loop


Top
#165028 - 2006-07-25 03:35 PM Re: Getting random elements from an array until all elements are done
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
Code:
$seed=SRND(@MSECS)

Dim $array[100]


; fill array
For $i = 0 to 100
$array[$i] = $i
Next

;shuffle array
For $i = 0 to 100
$new = Rnd(100)
$temp = $array[$i]
$array[$i] = $array[$new]
$array[$new] = $temp
Next

;show array
For $i = 0 to 100
? $i " = " $array[$i]
Next



This will shuffle the original array (0 - 100). The last for-next loop is just to show the shuffled array.

source

Top
#165029 - 2006-07-25 03:57 PM Re: Getting random elements from an array until all elements are done
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
Code:
Function ShuffleArray($)
dim $i, $r, $s, $t

$s=SRND(@MSECS)

For $i = 0 to UBound($)
$r = Rnd(ubound($))
$t = $[$i]
$[$i] = $[$r]
$[$r] = $t
Next

$ShuffleArray = $

EndFunction


Top
#165030 - 2006-07-25 04:17 PM Re: Getting random elements from an array until all elements are done
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Personally the way I do this is to randomly pick an item from the array then removed the selected item from the array. Just keep going until the array is empty.

There is an old deck-of-cards shuffler knocking about on the board somewhere if you are interested in using this technique.

Top
#165031 - 2006-07-25 09:21 PM Re: Getting random elements from an array until all elements are done
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Richard,

Great sugegstion.

This is what I have up until now. Maybe not the moste elegant way but it sort of works. I'm getting some empty elements returned that are in fact not empty lines in the file will have to look into that. This only seems to happen when the new random number and the array size are the same.

In this case the first element returned will always be the last element of the array but that is ok in this case.

I’ve been doing some test with just a 60 lines file. I’ll do some more testing when I’m back at work tomorrow and have access to the 18.490 lines file (it’s on a workstation that is turned off ).

Code:

Break on
;
$rc = SetOption ("WrapAtEOL", "on")
;
Call @SCRIPTDIR + "\RemFromArr().udf"
Call @SCRIPTDIR + "\ReadFile().udf"
;
$array = readfile("d:\test\links.txt")
$rand = Ubound($array)
$rc = SRND(0)
;
Do
?$array[$rand]
$array = Remfromarray($array, $array[$rand])
$rand = Ubound($array)
??"New array size: " $rand
Sleep 0.5
If $rand > "0"
$rand = Rnd($rand)
?"New random number: " $rand
Else
?"Array is empty."
EndIf
??
;Sleep 0.2
Until Ubound($array) = "0"
;
Sleep 3



Edited by Mart (2006-07-25 10:20 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#165032 - 2006-07-25 09:23 PM Re: Getting random elements from an array until all elements are done
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Sarg,

I’ll look into your suggestions at work tomorrow. Thanx.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#165033 - 2006-07-26 05:03 AM Re: Getting random elements from an array until all elements are done
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4562
Loc: USA
I can't say exactly why I found this so interesting but... here is what I came up with using ArrayPack.

Code:
break on
srnd(@ticks)
$rand=rnd(0)


$r=split("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z")


while ubound($r)>0
$rand = Rnd(ubound($r))
$r[$rand]
$r[$rand]=""
$r=arraypack($r)
loop
$r[0]






Function ArrayPack($array)
dim $loop, $arrTemp[0],$element
If Not VarType($Array) & 8192
Exit(1)
EndIf
$loop = -1
for each $element in $array
if $element > ' '
$loop = $loop + 1
redim preserve $arrTemp[$loop]
$arrTemp[$loop] = $element
endif
next
if ubound($arrTemp) = -1
Exit(2)
Endif
$ArrayPack = $arrTemp
endFunction



OK...so now I have a question. Is there a way to structure the loop so that the last element can be included without it generating an error. Seems that RND(0) is RND(32767), the default.


Edited by Allen (2006-07-26 05:06 AM)

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 1172 anonymous users online.
Newest Members
StuTheCoder, M_Moore, BeeEm, min_seow, Audio
17884 Registered Users

Generated in 0.065 seconds in which 0.031 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org