Shuffle objects

54 views
Skip to first unread message

Knives

unread,
Jul 30, 2010, 2:47:58 PM7/30/10
to Caché, Ensemble, DeepSee
Does anybody know of a way to shuffle objects in objectscript?
I want to randomize the objects in a %ListOfObjects

For example, in Java I would use Collections.Shuffle()

Pmkadow

unread,
Aug 1, 2010, 5:08:16 PM8/1/10
to intersystems...@googlegroups.com
I don't understand, what purpose does shuffling this List of Objects serve?
 
thanks
-Mike Kadow


--
InterSystems: Advanced software technologies for breakthrough applications

Caché 2010.1.2 released on May 29, 2010

servit

unread,
Aug 2, 2010, 4:43:01 AM8/2/10
to Caché, Ensemble, DeepSee
Try this:

#dim list As %ListOfDataTypes
s N=1e1
s list=##class(%ListOfDataTypes).%New()
for i=1:1:N d list.InsertAt(i,i)

w "Before Shuffle",!
for i=1:1:N {w list.GetAt(i)," "} w !

for i=1:1:N {
set old=list.GetAt(i)
set j=$random(N)+1
d list.SetAt(list.GetAt(j),i)
d list.SetAt(old,j)
}

w "After Shuffle",!
for i=1:1:N {w list.GetAt(i)," "} w !

PS: for %ListOfObjects - similarly.

Millions Knives

unread,
Aug 1, 2010, 10:29:37 PM8/1/10
to Caché, Ensemble, DeepSee
Shuffling the List of Objects would randomize the list in an ordered
way.

I don't want to pick a random object every time. I want to pick a
random object, then pick different random object. When I run out of
random objects, then I would rollover back to the first one i picked.
In a deck of cards, this would be analogous to shuffling the deck,
then drawing a card from the top then placing it back on the bottom
and repeating this process.

I can't tell you WHY I would want to do this, but that is the purpose
^_^

The only way I can think of to do this is to get the OREFs of all the
objects, and place them in some random order into an array. This way
is ugly, and I don't want to write obfuscated code if there is a
better way. I'm an ObjectScript n00b so I thought someone here might
know of a better way. Best not to reinvent the wheel.

Thanks!

Knives

servit

unread,
Aug 2, 2010, 9:01:45 AM8/2/10
to Caché, Ensemble, DeepSee
Hi, Millions Knives.

See documentation for class %Library.ListOfObjects (and his(its)
hierarchies):
http://docs.intersystems.com/cache20101/csp/documatic/%25CSP.Documatic.cls?PAGE=CLASS&LIBRARY=%25SYS&CLASSNAME=%25Library.ListOfObjects

Unfortunately built in method Shuffle() is not present.
Reply all
Reply to author
Forward
0 new messages