<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Hi YC, hi Will,</div><div><br></div><div>thanks a lot for your quick answers! I will try out your suggestions tomorrow at work where I have access to the data, but I would already like to ask some questions back now as I won't be able to write from the mail address I'm using with the PLT list&nbsp;</div><div>tomorrow during the day.</div><div><br></div><div>YC, I will try out the vyzo/crypto package thanks to your hint. I also wondered if I should use random-source-randomize! &nbsp;from SRFI-27. There is also an example in the docs for generating random permutations, which I could possibly use. Would you recommend trying this?</div><div><br></div><div>Regarding the point about drawing single cards, my concept (and the whole idea in the beginning quite some time ago...) was in fact to use amb, and I only added the "pre-shuffling" later when I realized that if I don't have enough constraints, amb will always choose the same person... Perhaps the initial idea about amb was not so good after all. Only now I think I won't have the time to rework the whole "algorithm" until the colleague gets impatient :-;</div><div>Anyway, from your suggestion about serializing the state between runs of the script it occurred to me that perhaps I don't need the pre-shuffle at all. I could just rotate the list at every run and serialize it - then at least it would be "fair".</div><div><br></div><div>Will, thanks for explaining the statistics. It's a stupid thing to ask, but... to check with an appropriate sample I really have to re-execute the script independently each time, correct? Because if I do it in a loop like I tried out quickly, the pseudo random number generator will use it's internal state, whereas what I need to know is the outcome on each first shuffle only? Sorry for asking such a simple thing, really...</div><div><br></div><div><br></div><div>Thanks again, I will check things out tomorrow,</div><div>Sigrid</div><div><br></div><div><br></div><br><div><div>Am 10.09.2009 um 21:29 schrieb YC:</div><br class="Apple-interchange-newline"><blockquote type="cite">Hi Sigrid, <br><br>even true random numbers do not have an uniform distribution if the run is small enough - to verify you just need to flip a fairly fair coin and count the number of times where head or tail shows up consecutively.&nbsp; <br> <br>If you suspect the issue is with the shuffling you should try to increase the test runs to see if you get a better distribution. <br><br>If the criteria of the rotation is "fairness" - i.e. everyone gets their fair shares of duty, I suspect you will have better luck by simulating card drawings - where each card drawn stays out of the deck, and reshuffles only when the deck is empty.&nbsp; <br> <br>You can persist the state between the drawings if you are not trying to draw all of the cards at once.&nbsp; <br><br>The drawing can itself be randomized - i.e. not always drawing from the top of the deck. &nbsp; <br><br>Checking for "not available" can be implemented as a redraw, or as a weighted drawing algorithm where you exclude the card from that particular draw.&nbsp; You can do the same to avoid consecutive weekday draws for any individual card. <br> <br>Lastly - PLT uses pseudo random number generators.&nbsp; You can try the vyzo/crypto package, which has a cryptographic random number generator. <br><br>Above are what I can think of on top of my head.&nbsp; HTH.<br>yc<br><br><div class="gmail_quote"> On Thu, Sep 10, 2009 at 10:49 AM, <a href="mailto:keydana@gmx.de">keydana@gmx.de</a> <span dir="ltr">&lt;<a href="mailto:keydana@gmx.de">keydana@gmx.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> <div style="">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" target="_blank">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&nbsp;<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 &nbsp;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&nbsp;<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>&nbsp;(lambda (start-date-string type)&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;(with-output-to-file (string-append logs-dir (duty-type-logfile type)) #:exists 'append<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(lambda ()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(printf "~nGoing to choose duty of type ~a for week starting from ~a~n" (duty-type-name type) start-date-string)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((from (string-to-date start-date-string)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (not (= (date-week-day from) 1))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(error "Start date has to be a Monday.")<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((to (get-date-for-interval from 4))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(previous-from (get-date-for-interval from -7)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((previous-to (get-date-for-interval previous-from 4)))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((known-events-list (get-events from to))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(previous-week-list (get-events previous-from previous-to)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((already-selected (extract-morning-duties known-events-list from to))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(unavailables (extract-unavailables known-events-list from to))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(previous-week-victims (flatten (extract-morning-duties previous-week-list previous-from previous-to))))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (all-true? (map (lambda (days-list) (not (null? days-list))) already-selected))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((week-of-year (get-week-of-year from)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(printf "Already chosen for morning duty: ~a~n" &nbsp;(map car already-selected))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(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> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((availables (map (lambda (lst) (complement *morning-duty-people* lst)) unavailables))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(previous-week-spared (complement *morning-duty-people* previous-week-victims)))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(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> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((possible-duties (choose-possible-duties availables already-selected previous-week-spared)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(when debug (printf "Choose-possible-duties output: ~s~n" possible-duties))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(check-unique possible-duties)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(when debug (printf "Unique duties are: ~s~n" possible-duties))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if dry-run<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(printf "Not sending events to calendar 'cause dry-run~n")<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((result (build-morning-duty-events possible-duties from)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((new-only (filter (lambda (event) (not (event-exists? known-events-list event))) result)))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(store-events new-only))))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((week-of-year (get-week-of-year from)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(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>&nbsp;(lambda (availables-list already-selected-list preferred-list)<br>&nbsp;&nbsp;&nbsp;(when debug (printf "Choose-possible-duties called with input:~nAvailable are: ~s~nAlready registered are: ~s~n" availables-list already-selected-list))<br> &nbsp;&nbsp;&nbsp;(let loop ((availables availables-list) (selected already-selected-list) (result '()))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (null? availables)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(reverse result)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((current-date-availables (car availables)) (current-date-selected (car selected)))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (not (null? current-date-selected))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((already-entered (car current-date-selected)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(loop (cdr availables) (cdr selected) (cons already-entered result)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let-values (((preferred&amp;available availablebutrathernot) (partition (lambda (person) (member person preferred-list)) current-date-availables)))<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let ((amb-result (grouped-list-amb preferred&amp;available availablebutrathernot)))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(loop (cdr availables) (cdr selected) (cons amb-result result))))))))))<br><br><br><br></div><br> _________________________________________________<br> &nbsp;For list-related administrative tasks:<br> &nbsp;<a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme" target="_blank">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br> <br></blockquote></div><br></blockquote></div><br></body></html>