Meine Common Lisp Kenntnisse sind zwar extrem bescheiden,
aber versuchen kann man's ja mal:
(SBCL)
Code: Alles auswählen
(defparameter *max-throws* 10000000)
(defun dices (n)
(let ((histo (make-array '(6))))
(dotimes (i n)
(let* ((r (random 6))
(x (aref histo r)))
(setf (aref histo r) (1+ x))))
histo))
(defun main ()
(let ((histo (dices *max-throws*)))
(dotimes (i 6)
(format t "~D: ~D~%" (1+ i) (aref histo i)))))
Ergibt
Code: Alles auswählen
CL-USER> (time (main))
1: 1667830
2: 1666404
3: 1664453
4: 1667586
5: 1665540
6: 1668187
Evaluation took:
0.407 seconds of real time
0.400025 seconds of user run time
0.0 seconds of system run time
0 calls to %EVAL
0 page faults and
32,768 bytes consed.
auf meinem uralten Athlon 2400.
Und weils so schön war hier mit SWI-Prolog:
Code: Alles auswählen
update([X|R], POS, POS, [Y|R]) :-
Y is X + 1.
update([X|R1], COUNT, POS, [X|R2]) :-
COUNT1 is COUNT + 1,
update(R1, COUNT1, POS, R2).
update([], _, _, []).
dices(N, L, HISTO) :-
N > 0,
X is random(6),
update(L, 0, X, L1),
N1 is N - 1,
dices(N1, L1, HISTO).
dices(_, L, L).
main(HISTO) :-
dices(90000, [0, 0, 0, 0, 0, 0], HISTO).
Code: Alles auswählen
?- time(main(X)).
% 990,635 inferences, 1.70 CPU in 1.83 seconds (93% CPU, 582726 Lips)
X = [15016, 14899, 15066, 14925, 14960, 15134]
Eine Rekursionstiefe von 1e7 schafft Prolog natürlich nicht
(out of local Stack), daher nur 90000 Durchläufe.
Auch erfolgt das Updaten des Histogramms mit O(n)
(n = Anzahl der Würfelseiten) statt O(1), aber ich dachte mir,
ich poste das einfach mal.
Ausreichend didaktisches Material muesste ja jetzt vorliegen.
yipyip