]> Git — Sourcephile - reloto-libreoffice.git/blob - tirages.py
Corrections d’orthographe et de cohérence.
[reloto-libreoffice.git] / tirages.py
1 # -*- coding: utf-8 -*-
2 # Author: Julien Moutinho <julm@autogeree.net>
3 # License: GNU GPLv3 (or later, at your choice)
4
5 from __future__ import unicode_literals
6 import uno
7 from com.sun.star.awt.MessageBoxType import MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX
8 from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_YES_NO, BUTTONS_YES_NO_CANCEL, BUTTONS_RETRY_CANCEL, BUTTONS_ABORT_IGNORE_RETRY
9 from com.sun.star.awt.MessageBoxResults import OK, YES, NO, CANCEL
10 from com.sun.star.beans import PropertyValue
11 from com.sun.star.sheet import CellFlags
12
13 if 'XSCRIPTCONTEXT' in globals():
14 def getModel():
15 return (XSCRIPTCONTEXT.getDocument(), XSCRIPTCONTEXT)
16 def debug(msg):
17 return
18 else:
19 from connect_to_libre_office import getModel
20 def debug(msg):
21 print(msg)
22
23 def MakeDraw(*args):
24 return
25
26 def nCk(n,k):
27 """
28 nCk(n,k) retourne le nombre de combinaisons
29 de longueur k d’un ensemble de longueur n.
30 """
31 if n<0 or k<0 or n<k:
32 raise ZeroDivisionError
33 if k > n // 2:
34 k = n - k # more efficient and safe with smaller numbers
35 c = 1
36 for j in range(1,k+1):
37 c = c * (n-j+1) // j
38 return c
39 def combinOfRank(n,k,rank):
40 """
41 combinOfRank(n, k, r) retourne les indices de permutation
42 de la combinaison de k entiers parmi [1..n]
43 au rang lexicographique r dans [0 .. nCk(n,k)-1].
44
45 Construit chaque choix de la combinaison en prenant le prochain plus grand
46 dont le successeur engendre un nombre de combinaisons
47 qui dépasse le rang restant à atteindre.
48
49 DOC: <http://www.site.uottawa.ca/~lucia/courses/5165-09/GenCombObj.pdf>, p.26
50 """
51 if rank<0 or nCk(n,k)<rank:
52 raise ZeroDivisionError
53 i = 1
54 j = 1
55 c = []
56 while True: # for1K
57 if i < k:
58 while True: # uptoRank
59 nbCombins = nCk(n-j, k-i)
60 if nbCombins > rank:
61 c.append(j)
62 i += 1
63 j += 1
64 break
65 else:
66 j += 1
67 rank -= nbCombins
68 elif i == k:
69 c.append(j+rank) # because when i == k, nbCombins is always 1
70 break
71 else:
72 break
73 return c
74 def rankOfCombin(n,ns):
75 """
76 rankOfCombin(n, ns) retourne le rang lexicographique dans [0 .. nCk(n, length ns)-1]
77 de la combinaison ns d’entiers parmi [1..n].
78
79 WARNING: ns doit être triée de manière ascendante.
80
81 Compte le nombre de combinaisons précédant celle de rang r.
82
83 DOC: <http://www.site.uottawa.ca/~lucia/courses/5165-09/GenCombObj.pdf>, pp.24-25
84
85 rankOfCombin(n, combinOfRank(n, k, r)) == r
86 combinOfRank(n, len(ns), rankOfCombin(n, ns)) == ns
87 """
88 if not all(1<=x or x<=n for x in ns):
89 raise ZeroDivisionError
90 k = len(ns)
91 if n<k:
92 raise ZeroDivisionError
93 i = 1
94 rank = 0
95 x1 = 0
96 for x in ns:
97 for j in range(x1+1,x-1 +1):
98 rank += nCk(n-j, k-i)
99 i += 1
100 x1 = x
101 return rank
102
103 def nbBits(n):
104 """
105 nbBits(n) retourne le nombre de bits servant à encoder n.
106 """
107 if n<0:
108 raise ZeroDivisionError
109 r = 0
110 while n > 0:
111 r += 1
112 n //= 2
113 return r
114 def equiprobableBits(n):
115 """
116 equiprobableBits(n) retourne le nombre maximal de bits de i
117 équiprobables quand i parcourt [0 .. n-1].
118
119 Ce nombre est le plus grand 'b' dans [0 .. ] tel que 2**b-1 <= n.
120
121 map(equiprobableBits, range(0, 17+1)) == [0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4]
122 """
123 b = nbBits(n)
124 return (b if n == 2**b-1 else b-1)
125 def bitsOfInteger(m, n):
126 """
127 bitsOfInteger(m, n) retourne les m premiers bits de poids faible
128 encodant le nombre n.
129 """
130 if not(0<=m and 0<=n):
131 raise ZeroDivisionError
132 bs = []
133 while m > 0:
134 (q,r) = (n//2, n%2)
135 bs.append(r==1)
136 m -= 1
137 n = q
138 return bs
139 def randomOfCombin(n, k, ns):
140 """
141 randomOfCombin(n, k, ns) retourne des bits équiprobables donnés
142 par la combinaison ns obtenue par tirage équiprobable
143 d’une combinaison de k entiers parmi [1 .. n].
144
145 WARNING: aucun bit n’est extrait du tirage ns
146 dans le cas où ns a un rang lexicographique encodé par
147 un nombre de bits strictement supérieur à equiprobableBits(nCk(n, k)).
148 """
149 if not(0<=n and 0<=k and k<=n and all(1<=x and x<=n for x in ns)):
150 raise ZeroDivisionError
151 ns.sort()
152 rank = rankOfCombin(n, ns)
153 epBits = equiprobableBits(nCk(n,k))
154 return bitsOfInteger(epBits, rank) \
155 if nbBits(rank) <= epBits else []
156
157 def randomOfFrenchLoto (n1,n2,n3,n4,n5, nc):
158 """
159 randomOfFrenchLoto(n1,n2,n3,n4,n5, numComplementaire) retourne les bits équiprobables donnés
160 par un tirage du Lo to Français : https://www.fdj.fr/jeux/jeux-de-tirage/loto/resultats/.
161
162 Il peut produire @23@ bits équiprobables :
163 sum(map(equiprobableBits, [nCk(49, 5), nCk(10, 1)]))
164
165 randomOfFrenchLoto(1,2,3,4,5, 1) == [False] * (20+3)
166 randomOfFrenchLoto(7,27,36,40,46, 8) == [True] * (20+3)
167
168 combinOfRank(49, 5, 2 ** equiprobableBits(nCk(49,5)) - 1) == [7,27,36,40,46]
169 combinOfRank(49, 5, 2 ** equiprobableBits(nCk(49,5))) == [7,27,36,40,47]
170 randomOfFrenchLoto(7,27,36,40,46, 1) == [True] * 20 + [False] * 3
171 randomOfFrenchLoto(7,27,36,40,47, 1) == [False,False,False]
172
173 combinOfRank(10, 1, 2 ** equiprobableBits(nCk(10, 1)) - 1) == [8]
174 combinOfRank(10, 1, 2 ** equiprobableBits(nCk(10, 1))) == [9]
175 randomOfFrenchLoto(7,27,36,40,47, 8) == [True,True,True]
176 randomOfFrenchLoto(7,27,36,40,47, 9) == []
177 """
178 return \
179 randomOfCombin(49, 5, [n1,n2,n3,n4,n5]) + \
180 randomOfCombin(10, 1, [nc])
181 def randomOfEuroMillions(n1,n2,n3,n4,n5, nc1,nc2):
182 """
183 https://www.fdj.fr/jeux/jeux-de-tirage/euromillions/resultats
184 """
185 return \
186 randomOfCombin(50, 5, [n1,n2,n3,n4,n5]) + \
187 randomOfCombin(11, 2, [nc1,nc2])
188 def randomOfSwissLoto(n1,n2,n3,n4,n5,n6, nc):
189 return \
190 randomOfCombin(42, 6 [n1,n2,n3,n4,n5,n6]) + \
191 randomOfCombin( 6, 1 [nc])
192 def randomOf6aus49(n1,n2,n3,n4,n5,n6, nc):
193 return \
194 randomOfCombin(49, 6, [n1,n2,n3,n4,n5,n6]) + \
195 randomOfCombin(10, 1, [nc])
196
197 g_exportedScripts = MakeDraw,