mirror of
				https://gitlab.crans.org/bde/nk20
				synced 2025-11-04 01:12:08 +01:00 
			
		
		
		
	[WEI] Store seed in WEI Survey to add determinism in RNG
Signed-off-by: Yohann D'ANELLO <ynerant@crans.org>
This commit is contained in:
		@@ -1,7 +1,7 @@
 | 
				
			|||||||
# 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):
 | 
				
			|||||||
        """
 | 
					        """
 | 
				
			||||||
        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):
 | 
				
			|||||||
    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):
 | 
				
			|||||||
    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()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user