# Randomizing from a written function

Hello,
I’m trying to convert my code from python using the PsychoPy library to the Builder.
I have a function (pasted below) I wrote to set-up specific numbers (for the experiment) in a list.

First, I need to run the function in the experiment and secondly, I need the list to be out-putted into excel so I can use the numbers in loading pictures.

Is it something possible to do?
and how?

thank you very much for the help
Gal

``````def random_options(a, steps, game_series) -> object:
a_reversed = [x[::-1] for x in a]
games = a + a_reversed

output = []  # Final output of games

# Number of 6-games
for i in range(1, (steps // game_series) + 1):

# Current 6-game
curr_output = []

# Copy game so we can remove from list and it will not remove from original list.
curr_games = copy.deepcopy(games)

# Loop for every 6-game
while len(curr_output) < game_series:
game = random.choice(curr_games)  # Choose randomly from list of 6 games

# Left condition: First game of this 6-game. Need to check last game of last 6-games
# Right condition: Only check this if not first game ever. If first game ever - don't check anything
if len(curr_output) == 0 and len(output) > 0:
if output[-1] == game or output[-1] == game[::-1]:
continue  # Randomly choose again

# Check letter of last game in this current 6-game
elif len(curr_output) > 0 and (curr_output[-1] == game or curr_output[-1] == game[::-1]):

# We already chose 5/6 games and can't choose the last one.
# It means we got stuck with 2 last games that can't be one after another
if len(curr_output) == (game_series - 1):
# Start over this 6-list
curr_output = []
curr_games = copy.deepcopy(games)
continue

# Add game to current 6-list
curr_output.append(game)

# Remove game from current 6-list so won't get chosen again
curr_games.remove(game)

output.extend(curr_output)

return output

options = dict()
options["block1"] = (random_options([(1, 3), (1, 2), (2, 3)], settings["timesteps"], settings["blockAvaliableOptions"] * 2), random_options([(1, 3), (1, 2), (2, 3)], settings["timesteps"], settings["blockAvaliableOptions"] * 2))
options["block2"] = (random_options([(4, 5), (4, 6), (5, 6)], settings["timesteps"], settings["blockAvaliableOptions"] * 2), random_options([(4, 5), (4, 6), (5, 6)], settings["timesteps"], settings["blockAvaliableOptions"] * 2))
options["block3"] = (random_options([(1, 4), (2, 5), (3, 6), (1, 6), (2, 6), (4, 3), (5, 3), (1, 5), (4, 2)], settings["timestepsTest"], settings["testAvaliableOptions"] * 2), random_options([(1, 4), (2, 5), (3, 6), (1, 6), (2, 6), (4, 3), (5, 3), (1, 5), (4, 2)], settings["timestepsTest"], settings["testAvaliableOptions"] * 2))
``````

Hi @galaharon, here is a simplified version of what you want (note, this uses Python libs and will not work with online tasks). Add the code to a Begin Experiment tab, and you can use the csv file it creates in a loop, which can handle your randomisation.

``````import pandas as pd

options = dict()
options["block1"] = [(1, 3), (1, 2), (2, 3)]
options["block2"] = [(1, 3), (1, 2), (2, 3)]
options["block3"] = [(1, 3), (1, 2), (2, 3)]

df = pd.DataFrame.from_dict(options)
df.to_csv('newList.csv', index=False)
``````

You can add “newList.csv” as a conditions file in a loop, and the variables / columns within the newly created csv file will be available for use in the experiment.