But the Rambam in chapter 23, law 17 wrote: "It is forbidden to cast lots or play with dice on Shabbat, because it is like buying and selling. But a person may cast lots with his sons and the members of his household for a large portion against a small portion, because they are not particular" thus far his words, and he permitted with his sons and the members of his household even a large portion against a small one, since it all belongs to him and it is nothing more than mere play and amusement. But the Tur forbids in such a case, as I wrote in the previous section, and they depend on the sugya of the Gemara, see there (see Beit Yosef and Taz subsection 4). And it is stated in the Gemara (149b) that they would cast lots in the Temple for the portions of the consecrated meats that reached them, because the eating of consecrated meats is a matter of a mitzvah, and they did not forbid lots for a matter of a mitzvah. Therefore, one may cast lots for consecrated items or for any other matter of a mitzvah, but not for a matter of a mitzvah - it is forbidden to cast lots, for this is like buying and selling. And even by means of a non-Jew - it is forbidden to cast lots on Shabbat, because the matter itself is like buying and selling. (And the Magen Avraham subsection 9 holds that even for a matter of a mitzvah, when he could have cast them yesterday - it is forbidden, see there; however, the proof he brought from there regarding weekday portions, that it is forbidden to cast lots in the Beit HaMikdash, is not a proof, because there the lot is truly not according to the law, for they do not divide sacrifices against sacrifices as the Tosafot wrote there, beginning with the words "Metilin," and it is only to quiet the quarrel, therefore they did not permit it for weekday portions, see there and examine it closely) (And the lot for a matter of a mitzvah that was permitted is not with notes, where each one draws a note, for this is certainly forbidden, but rather a lot by opening a book and seeing the letters as is customary) (And as for why there was no lot for the Showbread, which the Magen Avraham alluded to and thereby forbids lots on Shabbat, according to what I wrote it works out well, and examine it closely)
אבל הרמב"ם בפרק כ"ג דין י"ז כתב: "אסור להפיס ולשחק בקוביא בשבת, מפני שהוא כמקח וממכר. ומפיס אדם עם בניו ועם בני ביתו על מנה גדולה כנגד מנה קטנה, מפני שאין מקפידין" עכ"ל, והתיר בבניו ובני ביתו אפילו גדולה כנגד קטנה, כיון שהכל שלו ואינו אלא שחוק וגיעגועים בעלמא. אבל הטור אוסר בכהני גוונא, כמ"ש בסעיף הקודם, ותלוים בסוגית הגמרא ע"ש (עיין ב"י וט"ז סק"ד). ואיתא בגמרא (קמ"ט:) שהיו מטילים גורלות במקדש על חלקי הקדשים שהגיע להם, מפני שאכילת קדשים הוה דבר מצוה, ולא אסרו גורלות בדבר מצוה. ולכן מטילין גורל על הקדש או על שאר דבר מצוה, אבל שלא לדבר מצוה - אסור להטיל גורל, שזהו כעין מקח וממכר. ואפילו על ידי אינו יהודי - אסור להטיל גורל בשבת, משום דהענין עצמו הוה כמקח וממכר. (והמג"א סק"ט סובר דגם לדבר מצוה כשהיה יכול להטיל מאתמול - אסור ע"ש, אך הראיה שהביא משם ממנות של חול דאסור להטיל גורל בבית המקדש אינה ראיה, משום דהתם באמת אין הגורל כדין, שהרי אין חולקין זבחים כנגד זבחים כמ"ש התוספות שם ד"ה 'מטילין', ואינו אלא להשקיט הריב, לפיכך לא התירו על של חול ע"ש ודו"ק) (והגורל לדבר מצוה שהותר אינו בפתקאות, שכל אחד יעלה פיתקא, דזה ודאי אסור, אלא גורל בפתיחת הספר ולראות האותיות כנהוג) (ומה שלא היה גורל על לחם הפנים, דרמיז לה המג"א ואוסר גורל בשבת, לפי מ"ש אתי שפיר ודו"ק)