What i wanted:
A Sequence of 98 trials. The first 2 trials are random. Afterwards i have 3 restrictions:
- same number of each stimulus (there are four stimuli, so each 24 times)
- 25% of all transitions are stimulus repetitions; 75% are alternations
- artificial grammar for 2nd order transitions
Here is the solution:
from random import shuffle
from random import choice
seq = [1, 2, 3, 4]
pool = seq *24
shuffle(pool)
result = [choice(seq), choice(seq)]
i=0
j=0
double=0
print result, i, double, j
#print pool
while True:
while True:
if len(result)==98:
break
if result[i]==1 or result[i]==3:
if result[i+1]<3:
for j in range(len(pool)+1):
if pool[-j]>2: #second order restriction
if result[i+1] == pool[-j]: #repetition restriction
double=double+1 #count doublings
result.append(pool[-j])
pool.pop(-j)
i=i+1
j=0
break
elif pool[-j]<3: #go on searching in pool
if j==len(pool): #no solution found in pool
pool = seq *24
shuffle(pool)
result = [choice(seq), choice(seq)]
print i
i=0
double=0
j=0
elif j!=len(pool):
j=j+1
elif result[i+1]>2:
for j in range(len(pool)+1):
if pool[-j]<3: #second order restriction
if result[i+1] == pool[-j]: #repetition restriction
double=double+1 #count doublings
result.append(pool[-j])
pool.pop(-j)
i=i+1
j=0
break
elif pool[-j]>2: #go on searching in pool
if j==len(pool): #no solution found in pool
pool = seq *24
shuffle(pool)
result = [choice(seq), choice(seq)]
print i
i=0
double=0
j=0
elif j!=len(pool):
j=j+1
elif result[i]==2 or result[i]==4:
if result[i+1]<3:
for j in range(len(pool)+1):
if pool[-j]<3: #second order restriction
if result[i+1] == pool[-j]: #repetition restriction
double=double+1 #count doublings
result.append(pool[-j])
pool.pop(-j)
i=i+1
j=0
break
elif pool[-j]>2: #go on searching in pool
if j==len(pool): #no solution found in pool
pool = seq *24
shuffle(pool)
result = [choice(seq), choice(seq)]
print i
i=0
double=0
j=0
elif j!=len(pool):
j=j+1
elif result[i+1]>2:
for j in range(len(pool)+1):
if pool[-j]>2: #second order restriction
if result[i+1] == pool[-j]: #repetition restriction
double=double+1 #count doublings
result.append(pool[-j])
pool.pop(-j)
i=i+1
j=0
break
elif pool[-j]<3: #go on searching in pool
if j==len(pool): #no solution found in pool
pool = seq *24
shuffle(pool)
result = [choice(seq), choice(seq)]
print i
i=0
double=0
j=0
elif j!=len(pool):
j=j+1
#print result, len(result), i, double, j
#print pool
if double==24:
break
else:
pool = seq *24
shuffle(pool)
result = [choice(seq), choice(seq)]
print i, j
i=0
double=0
j=0
print result, len(result), double