unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Emanuel Berg <incal@dataswamp.org>
To: emacs-devel@gnu.org
Subject: another stats problem, example solution [stats.el]
Date: Mon, 04 Sep 2023 20:15:54 +0200	[thread overview]
Message-ID: <87h6o9pzxh.fsf@dataswamp.org> (raw)

Problem:

Return a list of N random numbers.
Every number is at least MIN.
The sum of the numbers is SUM.

Suggested solution, linear to SUM.

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/stats.el

(require 'cl-lib)

(defun random-distribution (n min sum)
  "Return a list of N random numbers.
Every number is at least MIN.
The sum of the numbers is SUM."
  (let ((min-share (* n min)))
    (unless (<= min-share sum)
      (error "Bogus indata, unsolvable") )
    (let ((vals (make-list n min))
          (pool (- sum min-share)) )
      (cl-loop while (and (< 0 pool)
                          (cl-decf pool) )
               do (cl-incf (nth (random n) vals)) )
      vals) ))

(defalias 'rd #'random-distribution)

;; (apply #'+ (rd 10 3 100)) ; 100
;;
;; (rd 10 10 100) ; border case check, but OK
;;
;; (rd 10 20 100) ; Bogus indata, unsolvable
;;
;; example distributions for n = 10, min = 3, and sum = 100:
;;   (12  8  8 10  7  6 19  9  7 14)
;;   ( 8 12  7 12  9 10  9  9 12 12)
;;   (14  6 15 11  9  9  6  8 11 11)
;;   ( 8 12  7  9 10  9 10 13 10 12)
;;   (12 12 15  6 10  8  6 10 14  7)
;;   ( 9  7 10  8 11 10  9 10 13 13)
;;   ( 7 13 13 10 13 10  5 13  5 11)
;;   (13  8  6  9 13  8 11  7 10 15)
;;   ( 7 10  5  6 17 14  8 10 14  9)
;;   (11 10  8  9 11 10 14 11  8  8)

(provide 'stats)

-- 
underground experts united
https://dataswamp.org/~incal




             reply	other threads:[~2023-09-04 18:15 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-04 18:15 Emanuel Berg [this message]
2023-09-04 20:08 ` another stats problem, example solution [stats.el] John Haman
2023-09-05  3:42   ` Emanuel Berg
2023-09-05 11:53     ` Philip Kaludercic
2023-09-06  0:21       ` Emanuel Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87h6o9pzxh.fsf@dataswamp.org \
    --to=incal@dataswamp.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).