Secret Santa : pick a pair [on hold]











up vote
0
down vote

favorite












I recently had to write some code to create a "Christmas Angel" list shuffler. Everyone in my school will have a secret person who makes little gifts, and will make gifts to another one.



There is one condition : when you shuffle the list, you must not pick your name, of course.



Here is my code :



import random

def indexes(array):
'''small function to create a blank array with len(array) empty items'''
output =
for i in range(0, len(array)):
output.append(i)

return output


liste = ["a", "b", "c", "d", "e"]
result = indexes(liste)
used_ind =


for i in range(0, len(liste)):

while True:
new_in = random.randrange(0, len(liste))
if new_in != i and new_in not in used_ind:
used_ind.append(new_in)
result[new_in] = liste[i]
break

print(result)


The code runs fine in python, but it sometimes gets stuck.
Any clue why ? I guess it must be unable to find the last index that has not been used yet, but the random generation is so quick that it eventually should find it quickly, no ?










share|improve this question







New contributor




Romain Schmid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Sᴀᴍ Onᴇᴌᴀ, alecxe, janos, IEatBagels, Dannnno 12 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Sᴀᴍ Onᴇᴌᴀ, alecxe, janos, IEatBagels, Dannnno

If this question can be reworded to fit the rules in the help center, please edit the question.













  • If it "gets stuck", then it does not "run fine". As such, this is not a question for Code Review; it's a question for Stack Overflow.
    – Reinderien
    14 hours ago










  • when you get to the last i (len(liste) - 1), if that index was not chosen before, all others were chosen so it will always happen that new_in == i or new_in in used_ind, making an infinite loop. The new_in != i condition doesn´t make much sense
    – juvian
    14 hours ago










  • To avoid the dead lock in the end you can shuffle the list first and then assign index i to (i+1). Think of a circle where everyone is an angel to its right neighbour.
    – stefan
    13 hours ago















up vote
0
down vote

favorite












I recently had to write some code to create a "Christmas Angel" list shuffler. Everyone in my school will have a secret person who makes little gifts, and will make gifts to another one.



There is one condition : when you shuffle the list, you must not pick your name, of course.



Here is my code :



import random

def indexes(array):
'''small function to create a blank array with len(array) empty items'''
output =
for i in range(0, len(array)):
output.append(i)

return output


liste = ["a", "b", "c", "d", "e"]
result = indexes(liste)
used_ind =


for i in range(0, len(liste)):

while True:
new_in = random.randrange(0, len(liste))
if new_in != i and new_in not in used_ind:
used_ind.append(new_in)
result[new_in] = liste[i]
break

print(result)


The code runs fine in python, but it sometimes gets stuck.
Any clue why ? I guess it must be unable to find the last index that has not been used yet, but the random generation is so quick that it eventually should find it quickly, no ?










share|improve this question







New contributor




Romain Schmid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Sᴀᴍ Onᴇᴌᴀ, alecxe, janos, IEatBagels, Dannnno 12 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Sᴀᴍ Onᴇᴌᴀ, alecxe, janos, IEatBagels, Dannnno

If this question can be reworded to fit the rules in the help center, please edit the question.













  • If it "gets stuck", then it does not "run fine". As such, this is not a question for Code Review; it's a question for Stack Overflow.
    – Reinderien
    14 hours ago










  • when you get to the last i (len(liste) - 1), if that index was not chosen before, all others were chosen so it will always happen that new_in == i or new_in in used_ind, making an infinite loop. The new_in != i condition doesn´t make much sense
    – juvian
    14 hours ago










  • To avoid the dead lock in the end you can shuffle the list first and then assign index i to (i+1). Think of a circle where everyone is an angel to its right neighbour.
    – stefan
    13 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I recently had to write some code to create a "Christmas Angel" list shuffler. Everyone in my school will have a secret person who makes little gifts, and will make gifts to another one.



There is one condition : when you shuffle the list, you must not pick your name, of course.



Here is my code :



import random

def indexes(array):
'''small function to create a blank array with len(array) empty items'''
output =
for i in range(0, len(array)):
output.append(i)

return output


liste = ["a", "b", "c", "d", "e"]
result = indexes(liste)
used_ind =


for i in range(0, len(liste)):

while True:
new_in = random.randrange(0, len(liste))
if new_in != i and new_in not in used_ind:
used_ind.append(new_in)
result[new_in] = liste[i]
break

print(result)


The code runs fine in python, but it sometimes gets stuck.
Any clue why ? I guess it must be unable to find the last index that has not been used yet, but the random generation is so quick that it eventually should find it quickly, no ?










share|improve this question







New contributor




Romain Schmid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I recently had to write some code to create a "Christmas Angel" list shuffler. Everyone in my school will have a secret person who makes little gifts, and will make gifts to another one.



There is one condition : when you shuffle the list, you must not pick your name, of course.



Here is my code :



import random

def indexes(array):
'''small function to create a blank array with len(array) empty items'''
output =
for i in range(0, len(array)):
output.append(i)

return output


liste = ["a", "b", "c", "d", "e"]
result = indexes(liste)
used_ind =


for i in range(0, len(liste)):

while True:
new_in = random.randrange(0, len(liste))
if new_in != i and new_in not in used_ind:
used_ind.append(new_in)
result[new_in] = liste[i]
break

print(result)


The code runs fine in python, but it sometimes gets stuck.
Any clue why ? I guess it must be unable to find the last index that has not been used yet, but the random generation is so quick that it eventually should find it quickly, no ?







python performance






share|improve this question







New contributor




Romain Schmid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Romain Schmid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Romain Schmid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 14 hours ago









Romain Schmid

12




12




New contributor




Romain Schmid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Romain Schmid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Romain Schmid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by Sᴀᴍ Onᴇᴌᴀ, alecxe, janos, IEatBagels, Dannnno 12 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Sᴀᴍ Onᴇᴌᴀ, alecxe, janos, IEatBagels, Dannnno

If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by Sᴀᴍ Onᴇᴌᴀ, alecxe, janos, IEatBagels, Dannnno 12 hours ago


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Sᴀᴍ Onᴇᴌᴀ, alecxe, janos, IEatBagels, Dannnno

If this question can be reworded to fit the rules in the help center, please edit the question.












  • If it "gets stuck", then it does not "run fine". As such, this is not a question for Code Review; it's a question for Stack Overflow.
    – Reinderien
    14 hours ago










  • when you get to the last i (len(liste) - 1), if that index was not chosen before, all others were chosen so it will always happen that new_in == i or new_in in used_ind, making an infinite loop. The new_in != i condition doesn´t make much sense
    – juvian
    14 hours ago










  • To avoid the dead lock in the end you can shuffle the list first and then assign index i to (i+1). Think of a circle where everyone is an angel to its right neighbour.
    – stefan
    13 hours ago


















  • If it "gets stuck", then it does not "run fine". As such, this is not a question for Code Review; it's a question for Stack Overflow.
    – Reinderien
    14 hours ago










  • when you get to the last i (len(liste) - 1), if that index was not chosen before, all others were chosen so it will always happen that new_in == i or new_in in used_ind, making an infinite loop. The new_in != i condition doesn´t make much sense
    – juvian
    14 hours ago










  • To avoid the dead lock in the end you can shuffle the list first and then assign index i to (i+1). Think of a circle where everyone is an angel to its right neighbour.
    – stefan
    13 hours ago
















If it "gets stuck", then it does not "run fine". As such, this is not a question for Code Review; it's a question for Stack Overflow.
– Reinderien
14 hours ago




If it "gets stuck", then it does not "run fine". As such, this is not a question for Code Review; it's a question for Stack Overflow.
– Reinderien
14 hours ago












when you get to the last i (len(liste) - 1), if that index was not chosen before, all others were chosen so it will always happen that new_in == i or new_in in used_ind, making an infinite loop. The new_in != i condition doesn´t make much sense
– juvian
14 hours ago




when you get to the last i (len(liste) - 1), if that index was not chosen before, all others were chosen so it will always happen that new_in == i or new_in in used_ind, making an infinite loop. The new_in != i condition doesn´t make much sense
– juvian
14 hours ago












To avoid the dead lock in the end you can shuffle the list first and then assign index i to (i+1). Think of a circle where everyone is an angel to its right neighbour.
– stefan
13 hours ago




To avoid the dead lock in the end you can shuffle the list first and then assign index i to (i+1). Think of a circle where everyone is an angel to its right neighbour.
– stefan
13 hours ago















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Ellipse (mathématiques)

Quarter-circle Tiles

Mont Emei