# Randomly sampling from two groups and presenting all sampled stimuli in random order

**URL:** <https://discourse.psychopy.org/t/randomly-sampling-from-two-groups-and-presenting-all-sampled-stimuli-in-random-order/11891>\
**Category:** Online experiments\
**Tags:** randomization, sound, pavlovia\
**Created:** [March 30, 2020, 5:48pm UTC](https://discourse.psychopy.org/t/randomly-sampling-from-two-groups-and-presenting-all-sampled-stimuli-in-random-order/11891 "2020-03-30T17:48:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![andersoc](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/andersoc/32/7332_2.png) [@andersoc](https://discourse.psychopy.org/u/andersoc)\
**Post date:** [March 30, 2020, 5:48pm UTC](https://discourse.psychopy.org/t/randomly-sampling-from-two-groups-and-presenting-all-sampled-stimuli-in-random-order/11891/1 "2020-03-30T17:48:58Z")

</div>

Description of the problem:

Because I’ve run into problems getting custom code components which do this to work on Pavlovia, I was wondering if there is a way to randomly sample stimuli from two different groups, and present them, one after another, in a loop while randomizing the order of the presentation.

In particular, I would like to sample two stimuli from one list (e.g., sampling two pictures of faces); two stimuli from another list (e.g., two pictures of plants) and present these four stimuli in a randomized order. I imagine there must be a way to do this in PsychoPy that also works in Pavlovia. I’ve attempted custom code components using Python and Javascript, but I can’t seem to get them working in PsychoJS. My experiment deals with sampling audio according to whether music is in a major or minor key.

The following code works in Python but not PsychoJS:

> [@How to present subset of all images randomly](https://discourse.psychopy.org/t/how-to-present-subset-of-all-images-randomly/2591/11):
>
> ```auto
> major = ("testMusic/Major1.wav", "testMusic/Major2.wav", "testMusic/Major3.wav", "testMusic/Major4.wav") #Make list of minor pieces minor = ("testMusic/Minor1.wav", "testMusic/Minor2.wav", "testMusic/Minor3.wav", "testMusic/Minor4.wav") #Randomly sample two pieces from each list major = np.random.choice(major, 2, replace = False) minor = np.random.choice(minor, 2, replace = False) ##--Other attempts from random module--## #major = random.sample(major, 2) #minor = random.sample(minor, 2) #concatenate dataframes frames = major + minor #sample 4 pieces: half major, half minor ## other attempt with random module: pracData = random.sample(frames, 4) pracData = np.random.shuffle(frames) #pracData = frames #assign pieces to variables pracPiece1 = pracData[0] pracPiece2 = pracData[1] pracPiece3 = pracData[2] pracPiece4 = pracData[3]
> 
> ```

This code seems to work in JS but runs into a problem when assigning the randomly sampled pieces to a soundfile component.

> [@How to present subset of all images randomly](https://discourse.psychopy.org/t/how-to-present-subset-of-all-images-randomly/2591/13):
>
> ```auto
> function getRandomSubarray(arr, size) { var shuffled = arr.slice(0), i = arr.length, temp, index; while (i--) { index = Math.floor((i + 1) * Math.random()); temp = shuffled[index]; shuffled[index] = shuffled[i]; shuffled[i] = temp; } return shuffled.slice(0, size); } var majorArray = ["testMusic/Major1.wav", "testMusic/Major2.wav", "testMusic/Major3", "testMusic/Major4.wav"]; var minorArray = ["testMusic/Minor1.wav", "testMusic/Minor2.wav", "testMusic/Minor3", "testMusic/Minor4.wav"]; var major = getRandomSubarray(majorArray, 2); var minor = getRandomSubarray(minorArray, 2); var frames = major.concat(minor); var pracData = getRandomSubarray(frames, 4); var pracPiece1 = pracData[0]; var pracPiece2 = pracData[1]; var pracPiece3 = pracData[2]; var pracPiece4 = pracData[3];
> 
> ```

Any suggestions/insights are much appreciated.
