lundi 6 août 2018

Give same random value to the random.seed() until a certain condition is met

def get_unanswered_questions(self, quiz,answers,correct_answers,lev):

    answered_questions = self.quiz_answers \
        .filter(answer__question__quiz=quiz) \
        .values_list('answer__question__pk', flat=True)
    answerss=list(answers)
    correct_answerss=list(correct_answers)
    list_of_ids= list(quiz.questions.values_list('id', flat=True))

    if len(answerss)==0:
          global rand_num
          rand_num=randint(0,1000)

    random.seed(rand_num)
    random.shuffle(list_of_ids)
    clauses = ' '.join(['WHEN id=%s THEN %s' % (pk, i) for i, pk in 
                 enumerate(list_of_ids)])
    ordering = 'CASE %s END' % clauses
    questions = quiz.questions.filter(pk__in=list_of_ids).exclude(pk__in=answered_questions).extra(select={'ordering': ordering }, order_by=('ordering', ))

I don't know the proper way of doing it. I need to generate a random number when the len(answerss)=0 and pass that same number to the random seed value so that the order of questions remain same for each execution of get_unanswered_questions until it gets to another condition when len(answerss)==0 again. Is there any way of doing it?




Aucun commentaire:

Enregistrer un commentaire