![]() |
![]() ![]() ![]() ![]() ![]()
|
next newest topic | next oldest topic |
Author | Topic: whats the simplest way of shuffling an array? |
cristian_vogel Member |
![]() ![]() ![]() I have a collection of samples in an array. I know how to reverse the array array := array reverse. But is there a nice simple way to shuffle the indices? IP: Logged |
SSC Administrator |
![]() ![]() ![]() If you simply want to access the elements of the array at random, you could generate a random index each time you wanted a new element. This does not, however, prevent you from selecting the same element more than once. To shuffle the elements in the array, think of the array as a deck of cards being shuffled by a dealer. On each shuffle, the dealer is permuting the order of the cards. We want to pick one of these permutations at random. Here's an example (if you copy/paste this into a text file in Kyma, you can select it and use ctrl+Y to evaluate it. Each time you evaluate it, you'll get a different random permutation of the array): code: Here is the same code but with comments and an extra variable for clarity: code: [This message has been edited by SSC (edited 10 February 2010).] IP: Logged |
cristian_vogel Member |
![]() ![]() ![]() nice thanks!
Also, what is PermutationStream on: ? Will this be documented somewhere? It's a very useful type of stream. [This message has been edited by cristian_vogel (edited 10 February 2010).] IP: Logged |
cristian_vogel Member |
![]() ![]() ![]() weirdness... try executing this with 10 elements - it hangs! - and around 9 elements goes real slow | ps r shuffled | IP: Logged |
SSC Administrator |
![]() ![]() ![]() I just added a new message, 'shuffle', that you can send to SequencebleCollections (like Arrays or Strings) in the upcoming release of Kyma (X.74) to do this more efficiently. For example code: Result = 'lloodehrwl' IP: Logged |
cristian_vogel Member |
![]() ![]() ![]() thats great. What I was intrigued by about this in Kyma, was the idea of shuffling an array of files, of kyma objects, perhaps like an array of running generators like ramps and bpms will this be possible, or will it be only strings, or only elements at compile time?
[This message has been edited by cristian_vogel (edited 10 February 2010).] IP: Logged |
SSC Administrator |
![]() ![]() ![]() "try executing this with 10 elements - it hangs! - and around 9 elements goes real slow" The number of permutations of an n-long array is n! To get an idea of why it goes 'real slow', try evaluating: 10 factorial in Kyma by selecting it and using ctrl+Y. (Hint the answer is 3,628,800). So probably best to hang in there for the next update which has a much more efficient solution. (Maybe you could use the PermutationStream version to experiment with short arrays in the meantime). IP: Logged |
cristian_vogel Member |
![]() ![]() ![]() Ouch! Yes. It had the feeling of a MASSIVE number! I was running it on a collection of filenames from a directory, so yes I can understand why Kyma went awry! thanks for thread, it is very educational IP: Logged |
SSC Administrator |
![]() ![]() ![]() quote: It works on Arrays or OrderedCollections of any elements (so yes, file names, CapyTalk code, etc). The shuffling is in Smalltalk so it takes place at compile time. But the elements could be CapyTalk expressions. For example, in the Samples field of a Multisample module, you could use: {'pottery break 1.aif' sampleFileNamesInSameFolder shuffle} [This message has been edited by SSC (edited 10 February 2010).] IP: Logged |
All times are CT (US) | next newest topic | next oldest topic |
![]() ![]() |
This forum is provided solely for the support and edification of the customers of Symbolic Sound Corporation.