Skip to content
Snippets Groups Projects
Verified Commit 43d214b9 authored by ynerant's avatar ynerant
Browse files

[WEI] Store seed in WEI Survey to add determinism in RNG


Signed-off-by: ynerant's avatarYohann D'ANELLO <ynerant@crans.org>
parent b93e4a8d
No related branches found
No related tags found
2 merge requests!172WEI, diverses améliorations,!171Améliorations WEI
Pipeline #8992 passed with warnings with stages
in 12 minutes and 16 seconds
# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay # Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
import time
from random import choice from random import Random
from django import forms from django import forms
from django.db import transaction from django.db import transaction
...@@ -35,7 +35,18 @@ class WEISurveyForm2021(forms.Form): ...@@ -35,7 +35,18 @@ class WEISurveyForm2021(forms.Form):
""" """
Filter the bus selector with the buses of the current WEI. Filter the bus selector with the buses of the current WEI.
""" """
words = [choice(WORDS) for _ in range(10)] information = WEISurveyInformation2021(registration)
if not information.seed:
information.seed = int(1000 * time.time())
information.save(registration)
registration.save()
rng = Random(information.seed)
words = []
for _ in range(information.step + 1):
# Generate N times words
words = [rng.choice(WORDS) for _ in range(10)]
words = [(w, w) for w in words] words = [(w, w) for w in words]
if self.data: if self.data:
self.fields["word"].choices = [(w, w) for w in WORDS] self.fields["word"].choices = [(w, w) for w in WORDS]
...@@ -59,6 +70,8 @@ class WEISurveyInformation2021(WEISurveyInformation): ...@@ -59,6 +70,8 @@ class WEISurveyInformation2021(WEISurveyInformation):
We store the id of the selected bus. We store only the name, but is not used in the selection: We store the id of the selected bus. We store only the name, but is not used in the selection:
that's only for humans that try to read data. that's only for humans that try to read data.
""" """
# Random seed that is stored at the first time to ensure that words are generated only once
seed = 0
step = 0 step = 0
def __init__(self, registration): def __init__(self, registration):
...@@ -125,5 +138,6 @@ class WEISurveyAlgorithm2021(WEISurveyAlgorithm): ...@@ -125,5 +138,6 @@ class WEISurveyAlgorithm2021(WEISurveyAlgorithm):
def run_algorithm(self): def run_algorithm(self):
for registration in self.get_registrations(): for registration in self.get_registrations():
survey = self.get_survey_class()(registration) survey = self.get_survey_class()(registration)
survey.select_bus(choice(Bus.objects.all())) rng = Random(survey.information.seed)
survey.select_bus(rng.choice(Bus.objects.all()))
survey.save() survey.save()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment