Page 1 of 1 1
Topic Options
#149784 - 2005-10-12 05:26 PM Select a random element from an array
xpanmanx Offline
Starting to like KiXtart

Registered: 2002-07-08
Posts: 108
Loc: St. Louis MO USA
Hi, I'm just learning to use arrays and I could use a spot of help.

I'm using Bryce's DirPlus() to build an array of all the files in a directory, then I'm trying to grab a random element of that array.

(I'm using Shawn's Randomize() to seed the RNG.)

I keep getting dinged out with "Error: Error in expression: this type of array not supported in expressions.!"

Will someone please give me a jump-start?

Code:
Break On

$dummy=SetOption('NoVarsInStrings','On')
$dummy=SetOption('Explicit','On')

Dim $FILE_ARRAY,$FILE_ARRAY_ELEMENT,$FILE_PATH

Randomize()

$File_Array=DirPlus('c:\windows\media') ;this directory is an example for the purposes of this post

$File_Array_Element=$File_Array(Rnd(Ubound($File_Array)))

? $File_Array($File_Array_Element) ;this is where the error occurs

Sleep 1

Quit



Here's the DirPlus() code in it's own post - I've not made any changes to the UDF in my script LINK

And here's the Randomize() code LINK

Best regards,

Tim ==

Top
#149785 - 2005-10-12 05:29 PM Re: Select a random element from an array
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
You need "[]", not "()":
Code:
$File_Array_Element=$File_Array[Rnd(Ubound($File_ARRAY))]
"Random element="+$File_Array_Element ?


Top
#149786 - 2005-10-12 05:47 PM Re: Select a random element from an array
xpanmanx Offline
Starting to like KiXtart

Registered: 2002-07-08
Posts: 108
Loc: St. Louis MO USA
Thanks, Richard - that took care of the error in fine style.

When running the code below, which is an evolution of my original post, $RANDOM behaves as expected, but $FILE always returns the first file in the directory. Why might this be happening?

Another oddity - it seems that my RND call is simply incrementing on successive executions, rather than generating random output. What's going on there?

Code:
Break On

$dummy=SetOption('NoVarsInStrings','On')
$dummy=SetOption('Explicit','On')

Dim $FILE_ARRAY,$RANDOM,$FILE_ARRAY_ELEMENT,$FILE

Randomize() ;Randomize() @ KiXtart.org - http://tinyurl.com/7ue38

$File_Array=DirPlus('c:\windows\media','/a-d') ;DirPlus() @ KiXtart.org - http://tinyurl.com/83qoy

$Random=Rnd(Ubound($File_Array))

$File_Array_Element=$File_Array[$Random]

$File=$File_Array[$File_Array_Element]

? 'Element=' + $Random

? 'File=' + $File

Sleep 0.5

Quit


Top
#149787 - 2005-10-12 05:52 PM Re: Select a random element from an array
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
I think this

Code:

$File_Array_Element=$File_Array[$Random]

$File=$File_Array[$File_Array_Element]



should just be

Code:

$File=$File_Array[$Random]


Top
#149788 - 2005-10-12 06:09 PM Re: Select a random element from an array
xpanmanx Offline
Starting to like KiXtart

Registered: 2002-07-08
Posts: 108
Loc: St. Louis MO USA
Yes, thank you very much.

Can anyone comment on why $RANDOM seems to increment, instead of 'randomizing?'

Top
#149789 - 2005-10-13 10:32 AM Re: Select a random element from an array
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Quote:

Yes, thank you very much.

Can anyone comment on why $RANDOM seems to increment, instead of 'randomizing?'




Try discarding the first random number as it is often not at all random.

This is not just an issue with KiXtart, it applies to certain pseudo random number generator algorithms.

Code:

$Random=Rnd() ; Discard first number - not very random!
$Random=Rnd(Ubound($File_Array))



Top
Page 1 of 1 1


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

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.123 seconds in which 0.08 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