Ehrenfest's urn - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: HP Prime (/forum-5.html) +--- Thread: Ehrenfest's urn (/thread-17871.html) Ehrenfest's urn - zahi48g - 12-29-2021 10:16 PM Ehrenfest's urn Hi Everyone (from France ) I'm trying this algorythm on my prime without success , of course i've tried to debug . EXPORT EHRENFEST1() BEGIN LOCAL n,p,K; INPUT(p); L1:={1,2}; L2:={}; FOR K FROM 1 TO p DO n:=RANDINT(1,2); IF L1(1)==n OR L1(2)==n THEN L1:=SUPPRESS(L1,n); L2:=INSERT(L2,1,n); ELSE L2:=SUPPRESS(L2,n); L1:=INSERT(L1,1,n); END; END; PRINT(L1); PRINT(L2); END; Could you help me ? It seems that the problem occurs when i write " n:=RANDINT(1,2); " . Thank you . RE: Ehrenfest's urn - toml_12953 - 12-29-2021 11:11 PM (12-29-2021 10:16 PM)zahi48g Wrote:  Ehrenfest's urn Hi Everyone (from France ) I'm trying this algorythm on my prime without success , of course i've tried to debug . EXPORT EHRENFEST1() BEGIN LOCAL n,p,K; INPUT(p); L1:={1,2}; L2:={}; FOR K FROM 1 TO p DO n:=RANDINT(1,2); IF L1(1)==n OR L1(2)==n THEN L1:=SUPPRESS(L1,n); L2:=INSERT(L2,1,n); ELSE L2:=SUPPRESS(L2,n); L1:=INSERT(L1,1,n); END; END; PRINT(L1); PRINT(L2); END; Could you help me ? It seems that the problem occurs when i write " n:=RANDINT(1,2); " . Thank you . L2 only has one element. If n=2 you get an error when you try to SUPPRESS L2(n) since L2(2) doesn't exist. RE: Ehrenfest's urn - zahi48g - 12-29-2021 11:53 PM Hi Thank you for replying. First , the algorithme is from a book, I've just translated it into the prime language . Second , it does not try to remove L2(2) from L2 but n from L2 . According to the Ehrenfest'urns model an urn can be empty (this is anticipated in the IF...THEN...ELSE), so L2(2) may not exist . I should have the following outcomes : {1,2} {} or {1} {2} or {} {1,2} or {2} {1} If you get these results , could copy and paste you code ? Thank you . RE: Ehrenfest's urn - Didier Lachieze - 12-30-2021 05:28 AM (12-29-2021 11:53 PM)zahi48g Wrote:  it does not try to remove L2(2) from L2 but n from L2 . With SUPPRESS(L2,n) you are removing from L2 the element at the nth position, so if L2={2} then SUPRESS(L2,2) will fail because there is only one element in the list. The correct instruction to remove 2 from the list is SUPPRESS(L2,POS(L2,2))) which will remove 2 from the list whatever it’s position in the list. (12-29-2021 11:53 PM)zahi48g Wrote:  If you get these results , could copy and paste you code ? Here is a working code, I’ve added the number of elements in the urns as a parameter of the program so you can test with more than two: Code: ```#pragma mode( separator(.,;) integer(h32) ) EXPORT EHRENFEST1(N) BEGIN  LOCAL n,p,K;  INPUT(p);  L1:=MAKELIST(I,I,1,N);  L2:={};  FOR K FROM 1 TO p DO   n:=RANDINT(1,N);   IF POS(L1,n) THEN    L1:=SUPPRESS(L1,POS(L1,n));    L2(0):=n;   ELSE    L2:=SUPPRESS(L2,POS(L2,n));    L1(0):=n;   END;  END;  PRINT(SORT(L1));  PRINT(SORT(L2)); END;``` RE: Ehrenfest's urn - John Keith - 12-30-2021 10:30 PM Thanks for the interesting post, this problem is new to me. Section 2.1 of this article suggests an alternate representation of the urns that may lead to a simpler program. Instead of two lists, one initially empty, one has a single list of (initially) all zeros. At each iteration, the state of a randomly selected element is flipped using NOT. The number of balls in urn "A" is the number of zeros in the list; the number of balls in urn "B" is similarly the number of ones. My PPL is a bit rusty but it should be fairly simple. RE: Ehrenfest's urn - zahi48g - 01-03-2022 11:10 PM Thank you D Lachieze and John Keith. I've learned two things : How to remove properly an object from a list and that L1(0) refers to the last element of list L1 (am I right ?) . But i've got one question for D Lachieze . In order to know if n belongs to L1, shouldn't you write " IF POS(L1,n)>0 THEN ..." instead of " IF POS(L1,n) THEN ..." ? Thank you RE: Ehrenfest's urn - toml_12953 - 01-04-2022 02:09 AM (01-03-2022 11:10 PM)zahi48g Wrote:  Thank you D Lachieze and John Keith. I've learned two things : How to remove properly an object from a list and that L1(0) refers to the last element of list L1 (am I right ?) . But i've got one question for D Lachieze . In order to know if n belongs to L1, shouldn't you write " IF POS(L1,n)>0 THEN ..." instead of " IF POS(L1,n) THEN ..." ? Thank you Since false is 0 and the true condition is anything other than 0, either test works.