<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi,<br><br>I am in a rather uncomfortable situation after writing a script to select people for the (not too popular) rotating job of "duty person" in our department...<br>One colleague complains he is always chosen on Monday. Up till now the sample is very small, which would make "proving" this hypothesis impossible, but it's not a nice situation all the same, so I would like to make "as sure as possible" that the distribution is random.<br><br>The part of the script in question works on a weekly basis, generating a different "duty person" for every day of a week. It gets any known "unavailability events" for the week from our calendar and filters the input list accordingly. Then, for every day, it divides the list of people who are available into two parts, those who did duty the week before (list A) and those who did not (list B). It then uses the amb operator to select a person for every day (separately), preferredly from list B but if necessary also from list A. In the end, it checks that the 5 people selected are unique, else it will backtrack.<br><br>Now because it was clear that amb would backtrack in a foreseeable way, when the script starts it first shuffles the input list. For the shuffle, I am using the function P. Ragde posted here (see<a href="http://groups.google.com/group/plt-scheme/browse_thread/thread/2b0de444dc77e11f">http://groups.google.com/group/plt-scheme/browse_thread/thread/2b0de444dc77e11f</a>, at the end), which was said to have a good random distribution (I also can confirm this when I run it e.g. 1000 times in a row).<br>However, in my script I run the shuffle only once per execution, and when I e.g. run the whole script 20 times and look who is first in the shuffled list, I get the following distribution (the numbers stand for the position of the collegue in the input list):<br><br>0 xxxxx<br>1 x<br>2 xxx<br>3 xx<br>4 xx<br>5 xx<br>6 x<br>7 <br>8 x<br><br>Unfortunately this looks like a correlation, and the aforementioned colleague is the one above :-;<br><br>However, I do not understand this, as the mentioned shuffle function uses random, and as I read in the documentation random is seeded with current-milliseconds... So it really should get a different seed at every execution.<br><br>I think that somewhere I must be making a mistake here... I would be very grateful for any hints.<br>I attach a bit of the code, hopefully as a clarification of how the selection works, although it is not runnable without the rest and the data provider of course.<br><br>Thanks a lot <br>Sigrid<br><br><br>(define *morning-duty-people* (shuffle '(AG MB SK MM AK RA AV CM HM)))<br><br>(define make-morning-duty-list<br> (lambda (start-date-string type) <br> (with-output-to-file (string-append logs-dir (duty-type-logfile type)) #:exists 'append<br> (lambda ()<br> (printf "~nGoing to choose duty of type ~a for week starting from ~a~n" (duty-type-name type) start-date-string)<br> (let ((from (string-to-date start-date-string)))<br> (if (not (= (date-week-day from) 1))<br> (error "Start date has to be a Monday.")<br> (let ((to (get-date-for-interval from 4))<br> (previous-from (get-date-for-interval from -7)))<br> (let ((previous-to (get-date-for-interval previous-from 4)))<br> (let ((known-events-list (get-events from to))<br> (previous-week-list (get-events previous-from previous-to)))<br> (let ((already-selected (extract-morning-duties known-events-list from to))<br> (unavailables (extract-unavailables known-events-list from to))<br> (previous-week-victims (flatten (extract-morning-duties previous-week-list previous-from previous-to))))<br> (if (all-true? (map (lambda (days-list) (not (null? days-list))) already-selected))<br> (let ((week-of-year (get-week-of-year from)))<br> (printf "Already chosen for morning duty: ~a~n" (map car already-selected))<br> (send-email *SERVER* *MAIL-TO* *SENDER* (if dry-run (format "DRY RUN: ~a ~a" (duty-type-mail-subject type) week-of-year) (format "~a ~a" (duty-type-mail-subject type) week-of-year)) (insert-variables (duty-type-mail-body-exists type) week-of-year (date-to-string from 'short) (date-to-string to 'short) (map car already-selected))))<br> (let ((availables (map (lambda (lst) (complement *morning-duty-people* lst)) unavailables))<br> (previous-week-spared (complement *morning-duty-people* previous-week-victims)))<br> (when debug (printf "Going to determine morning duties for period from ~a to ~a:~nUnavailable are: ~s~nAlready entered are: ~s~nChoosable are: ~s~nPrevious week's victims were: ~s~n" (date->string from "~Y-~m-~d") (date->string to "~Y-~m-~d") unavailables already-selected availables previous-week-victims))<br> (let ((possible-duties (choose-possible-duties availables already-selected previous-week-spared)))<br> (when debug (printf "Choose-possible-duties output: ~s~n" possible-duties))<br> (check-unique possible-duties)<br> (when debug (printf "Unique duties are: ~s~n" possible-duties))<br> (if dry-run<br> (printf "Not sending events to calendar 'cause dry-run~n")<br> (let ((result (build-morning-duty-events possible-duties from)))<br> (let ((new-only (filter (lambda (event) (not (event-exists? known-events-list event))) result)))<br> (store-events new-only))))<br> (let ((week-of-year (get-week-of-year from)))<br> (send-email *SERVER* *MAIL-TO* *SENDER* (if dry-run (format "DRY RUN: ~a ~a" (duty-type-mail-subject type) week-of-year) (format "~a ~a" (duty-type-mail-subject type) week-of-year)) (insert-variables (duty-type-mail-body type) week-of-year (date-to-string from 'short) (date-to-string to 'short) possible-duties))))))))))))))))<br><br><br>(define choose-possible-duties<br> (lambda (availables-list already-selected-list preferred-list)<br> (when debug (printf "Choose-possible-duties called with input:~nAvailable are: ~s~nAlready registered are: ~s~n" availables-list already-selected-list))<br> (let loop ((availables availables-list) (selected already-selected-list) (result '()))<br> (if (null? availables)<br> (reverse result)<br> (let ((current-date-availables (car availables)) (current-date-selected (car selected)))<br> (if (not (null? current-date-selected))<br> (let ((already-entered (car current-date-selected)))<br> (loop (cdr availables) (cdr selected) (cons already-entered result)))<br> (let-values (((preferred&available availablebutrathernot) (partition (lambda (person) (member person preferred-list)) current-date-availables)))<br> (let ((amb-result (grouped-list-amb preferred&available availablebutrathernot)))<br> (loop (cdr availables) (cdr selected) (cons amb-result result))))))))))<br><br><br><br></body></html>