给random.seed()提供相同的随机值,直到满足特定条件为止

时间:2018-08-07 05:16:29

标签: django python-3.x random

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', ))

我不知道这样做的正确方法。我需要在 len(answerss)= 0 时生成一个随机数,并将该数字传递给随机种子值,以便每次执行 get_unanswered_questions 时问题的顺序保持不变强>  我只需要在测验开始后就生成随机问题,并按照随机顺序获得问题 直到再次达到 len(answerss)== 0 为止。

  

函数 get_unanswered_questions 的每次多次执行都会获得随机数   有什么办法吗?

0 个答案:

没有答案
相关问题